| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "net/ftp/ftp_ctrl_response_buffer.h" | 5 #include "net/ftp/ftp_ctrl_response_buffer.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 | 10 |
| 11 namespace net { | 11 namespace net { |
| 12 | 12 |
| 13 // static | 13 // static |
| 14 const int FtpCtrlResponse::kInvalidStatusCode = -1; | 14 const int FtpCtrlResponse::kInvalidStatusCode = -1; |
| 15 | 15 |
| 16 int FtpCtrlResponseBuffer::ConsumeData(const char* data, int data_length) { | 16 int FtpCtrlResponseBuffer::ConsumeData(const char* data, int data_length) { |
| 17 buffer_.append(data, data_length); | 17 buffer_.append(data, data_length); |
| 18 ExtractFullLinesFromBuffer(); | 18 ExtractFullLinesFromBuffer(); |
| 19 | 19 |
| 20 while (!lines_.empty()) { | 20 while (!lines_.empty()) { |
| 21 ParsedLine line = lines_.front(); | 21 ParsedLine line = lines_.front(); |
| 22 lines_.pop(); | 22 lines_.pop(); |
| 23 | 23 |
| 24 if (line_buf_.empty()) { | 24 if (multiline_) { |
| 25 if (!line.is_complete) | |
| 26 return ERR_INVALID_RESPONSE; | |
| 27 | |
| 28 response_buf_.status_code = line.status_code; | |
| 29 if (line.is_multiline) { | |
| 30 line_buf_ = line.status_text; | |
| 31 } else { | |
| 32 response_buf_.lines.push_back(line.status_text); | |
| 33 responses_.push(response_buf_); | |
| 34 | |
| 35 // Prepare to handle following lines. | |
| 36 response_buf_ = FtpCtrlResponse(); | |
| 37 line_buf_.clear(); | |
| 38 } | |
| 39 } else { | |
| 40 if (!line.is_complete || line.status_code != response_buf_.status_code) { | 25 if (!line.is_complete || line.status_code != response_buf_.status_code) { |
| 41 line_buf_.append(line.raw_text); | 26 line_buf_.append(line.raw_text); |
| 42 continue; | 27 continue; |
| 43 } | 28 } |
| 44 | 29 |
| 45 response_buf_.lines.push_back(line_buf_); | 30 response_buf_.lines.push_back(line_buf_); |
| 46 | 31 |
| 47 line_buf_ = line.status_text; | 32 line_buf_ = line.status_text; |
| 48 DCHECK_EQ(line.status_code, response_buf_.status_code); | 33 DCHECK_EQ(line.status_code, response_buf_.status_code); |
| 49 | 34 |
| 50 if (!line.is_multiline) { | 35 if (!line.is_multiline) { |
| 51 response_buf_.lines.push_back(line_buf_); | 36 response_buf_.lines.push_back(line_buf_); |
| 52 responses_.push(response_buf_); | 37 responses_.push(response_buf_); |
| 53 | 38 |
| 54 // Prepare to handle following lines. | 39 // Prepare to handle following lines. |
| 55 response_buf_ = FtpCtrlResponse(); | 40 response_buf_ = FtpCtrlResponse(); |
| 56 line_buf_.clear(); | 41 line_buf_.clear(); |
| 42 multiline_ = false; |
| 43 } |
| 44 } else { |
| 45 if (!line.is_complete) |
| 46 return ERR_INVALID_RESPONSE; |
| 47 |
| 48 response_buf_.status_code = line.status_code; |
| 49 if (line.is_multiline) { |
| 50 line_buf_ = line.status_text; |
| 51 multiline_ = true; |
| 52 } else { |
| 53 response_buf_.lines.push_back(line.status_text); |
| 54 responses_.push(response_buf_); |
| 55 |
| 56 // Prepare to handle following lines. |
| 57 response_buf_ = FtpCtrlResponse(); |
| 58 line_buf_.clear(); |
| 57 } | 59 } |
| 58 } | 60 } |
| 59 } | 61 } |
| 60 | 62 |
| 61 return OK; | 63 return OK; |
| 62 } | 64 } |
| 63 | 65 |
| 64 // static | 66 // static |
| 65 FtpCtrlResponseBuffer::ParsedLine FtpCtrlResponseBuffer::ParseLine( | 67 FtpCtrlResponseBuffer::ParsedLine FtpCtrlResponseBuffer::ParseLine( |
| 66 const std::string& line) { | 68 const std::string& line) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 94 for (size_t i = 0; i < buffer_.length(); i++) { | 96 for (size_t i = 0; i < buffer_.length(); i++) { |
| 95 if (i >= 1 && buffer_[i - 1] == '\r' && buffer_[i] == '\n') { | 97 if (i >= 1 && buffer_[i - 1] == '\r' && buffer_[i] == '\n') { |
| 96 lines_.push(ParseLine(buffer_.substr(cut_pos, i - cut_pos - 1))); | 98 lines_.push(ParseLine(buffer_.substr(cut_pos, i - cut_pos - 1))); |
| 97 cut_pos = i + 1; | 99 cut_pos = i + 1; |
| 98 } | 100 } |
| 99 } | 101 } |
| 100 buffer_.erase(0, cut_pos); | 102 buffer_.erase(0, cut_pos); |
| 101 } | 103 } |
| 102 | 104 |
| 103 } // namespace net | 105 } // namespace net |
| OLD | NEW |