| 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_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 //#include "base/string_util.h" | 9 //#include "base/string_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 // static | 14 // static |
| 15 const int FtpCtrlResponse::kInvalidStatusCode = -1; | 15 const int FtpCtrlResponse::kInvalidStatusCode = -1; |
| 16 | 16 |
| 17 FtpCtrlResponse::FtpCtrlResponse() : status_code(kInvalidStatusCode) {} |
| 18 |
| 19 FtpCtrlResponse::~FtpCtrlResponse() {} |
| 20 |
| 21 FtpCtrlResponseBuffer::FtpCtrlResponseBuffer() : multiline_(false) {} |
| 22 |
| 23 FtpCtrlResponseBuffer::~FtpCtrlResponseBuffer() {} |
| 24 |
| 17 int FtpCtrlResponseBuffer::ConsumeData(const char* data, int data_length) { | 25 int FtpCtrlResponseBuffer::ConsumeData(const char* data, int data_length) { |
| 18 buffer_.append(data, data_length); | 26 buffer_.append(data, data_length); |
| 19 ExtractFullLinesFromBuffer(); | 27 ExtractFullLinesFromBuffer(); |
| 20 | 28 |
| 21 while (!lines_.empty()) { | 29 while (!lines_.empty()) { |
| 22 ParsedLine line = lines_.front(); | 30 ParsedLine line = lines_.front(); |
| 23 lines_.pop(); | 31 lines_.pop(); |
| 24 | 32 |
| 25 if (multiline_) { | 33 if (multiline_) { |
| 26 if (!line.is_complete || line.status_code != response_buf_.status_code) { | 34 if (!line.is_complete || line.status_code != response_buf_.status_code) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 57 // Prepare to handle following lines. | 65 // Prepare to handle following lines. |
| 58 response_buf_ = FtpCtrlResponse(); | 66 response_buf_ = FtpCtrlResponse(); |
| 59 line_buf_.clear(); | 67 line_buf_.clear(); |
| 60 } | 68 } |
| 61 } | 69 } |
| 62 } | 70 } |
| 63 | 71 |
| 64 return OK; | 72 return OK; |
| 65 } | 73 } |
| 66 | 74 |
| 75 FtpCtrlResponse FtpCtrlResponseBuffer::PopResponse() { |
| 76 FtpCtrlResponse result = responses_.front(); |
| 77 responses_.pop(); |
| 78 return result; |
| 79 } |
| 80 |
| 81 FtpCtrlResponseBuffer::ParsedLine::ParsedLine() |
| 82 : has_status_code(false), |
| 83 is_multiline(false), |
| 84 is_complete(false), |
| 85 status_code(FtpCtrlResponse::kInvalidStatusCode) { |
| 86 } |
| 87 |
| 67 // static | 88 // static |
| 68 FtpCtrlResponseBuffer::ParsedLine FtpCtrlResponseBuffer::ParseLine( | 89 FtpCtrlResponseBuffer::ParsedLine FtpCtrlResponseBuffer::ParseLine( |
| 69 const std::string& line) { | 90 const std::string& line) { |
| 70 ParsedLine result; | 91 ParsedLine result; |
| 71 | 92 |
| 72 if (line.length() >= 3) { | 93 if (line.length() >= 3) { |
| 73 if (base::StringToInt(line.substr(0, 3), &result.status_code)) | 94 if (base::StringToInt(line.substr(0, 3), &result.status_code)) |
| 74 result.has_status_code = (100 <= result.status_code && | 95 result.has_status_code = (100 <= result.status_code && |
| 75 result.status_code <= 599); | 96 result.status_code <= 599); |
| 76 if (result.has_status_code && line.length() >= 4 && line[3] == ' ') { | 97 if (result.has_status_code && line.length() >= 4 && line[3] == ' ') { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 97 for (size_t i = 0; i < buffer_.length(); i++) { | 118 for (size_t i = 0; i < buffer_.length(); i++) { |
| 98 if (i >= 1 && buffer_[i - 1] == '\r' && buffer_[i] == '\n') { | 119 if (i >= 1 && buffer_[i - 1] == '\r' && buffer_[i] == '\n') { |
| 99 lines_.push(ParseLine(buffer_.substr(cut_pos, i - cut_pos - 1))); | 120 lines_.push(ParseLine(buffer_.substr(cut_pos, i - cut_pos - 1))); |
| 100 cut_pos = i + 1; | 121 cut_pos = i + 1; |
| 101 } | 122 } |
| 102 } | 123 } |
| 103 buffer_.erase(0, cut_pos); | 124 buffer_.erase(0, cut_pos); |
| 104 } | 125 } |
| 105 | 126 |
| 106 } // namespace net | 127 } // namespace net |
| OLD | NEW |