OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_HTTP_HTTP_STREAM_PARSER_H_ |
| 6 #define NET_HTTP_HTTP_STREAM_PARSER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/upload_data_stream.h" |
| 13 #include "net/http/http_chunked_decoder.h" |
| 14 #include "net/http/http_response_info.h" |
| 15 #include "net/socket/client_socket_handle.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 class ClientSocketHandle; |
| 20 class HttpRequestInfo; |
| 21 |
| 22 class HttpStreamParser { |
| 23 public: |
| 24 // Any data in |read_buffer| will be used before reading from the socket |
| 25 // and any data left over after parsing the stream will be put into |
| 26 // |read_buffer|. The left over data will start at offset 0 and the |
| 27 // buffer's offset will be set to the first free byte. |read_buffer| may |
| 28 // have its capacity changed. |
| 29 HttpStreamParser(ClientSocketHandle* connection, |
| 30 GrowableIOBuffer* read_buffer); |
| 31 ~HttpStreamParser() {} |
| 32 |
| 33 // These functions implement the interface described in HttpStream with |
| 34 // some additional functionality |
| 35 int SendRequest(const HttpRequestInfo* request, const std::string& headers, |
| 36 UploadDataStream* request_body, CompletionCallback* callback); |
| 37 |
| 38 int ReadResponseHeaders(CompletionCallback* callback); |
| 39 |
| 40 int ReadResponseBody(IOBuffer* buf, int buf_len, |
| 41 CompletionCallback* callback); |
| 42 |
| 43 uint64 GetUploadProgress() const; |
| 44 |
| 45 HttpResponseInfo* GetResponseInfo(); |
| 46 |
| 47 bool IsResponseBodyComplete() const; |
| 48 |
| 49 bool CanFindEndOfResponse() const; |
| 50 |
| 51 bool IsMoreDataBuffered() const; |
| 52 |
| 53 private: |
| 54 // FOO_COMPLETE states implement the second half of potentially asynchronous |
| 55 // operations and don't necessarily mean that FOO is complete. |
| 56 enum State { |
| 57 STATE_NONE, |
| 58 STATE_SENDING_HEADERS, |
| 59 STATE_SENDING_BODY, |
| 60 STATE_REQUEST_SENT, |
| 61 STATE_READ_HEADERS, |
| 62 STATE_READ_HEADERS_COMPLETE, |
| 63 STATE_BODY_PENDING, |
| 64 STATE_READ_BODY, |
| 65 STATE_READ_BODY_COMPLETE, |
| 66 STATE_DONE |
| 67 }; |
| 68 |
| 69 // The number of bytes by which the header buffer is grown when it reaches |
| 70 // capacity. |
| 71 enum { kHeaderBufInitialSize = 4096 }; |
| 72 |
| 73 // |kMaxHeaderBufSize| is the number of bytes that the response headers can |
| 74 // grow to. If the body start is not found within this range of the |
| 75 // response, the transaction will fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 76 // Note: |kMaxHeaderBufSize| should be a multiple of |kHeaderBufInitialSize|. |
| 77 enum { kMaxHeaderBufSize = 256 * 1024 }; // 256 kilobytes. |
| 78 |
| 79 // Handle callbacks. |
| 80 void OnIOComplete(int result); |
| 81 |
| 82 // Try to make progress sending/receiving the request/response. |
| 83 int DoLoop(int result); |
| 84 |
| 85 // The implementations of each state of the state machine. |
| 86 int DoSendHeaders(int result); |
| 87 int DoSendBody(int result); |
| 88 int DoReadHeaders(); |
| 89 int DoReadHeadersComplete(int result); |
| 90 int DoReadBody(); |
| 91 int DoReadBodyComplete(int result); |
| 92 |
| 93 // Examines |read_buf_| to find the start and end of the headers. Return |
| 94 // the offset for the end of the headers, or -1 if the complete headers |
| 95 // were not found. If they are are found, parse them with |
| 96 // DoParseResponseHeaders(). |
| 97 int ParseResponseHeaders(); |
| 98 |
| 99 // Parse the headers into response_. |
| 100 void DoParseResponseHeaders(int end_of_header_offset); |
| 101 |
| 102 // Examine the parsed headers to try to determine the response body size. |
| 103 void CalculateResponseBodySize(); |
| 104 |
| 105 // Current state of the request. |
| 106 State io_state_; |
| 107 |
| 108 // The request to send. |
| 109 const HttpRequestInfo* request_; |
| 110 |
| 111 // The request header data. |
| 112 scoped_refptr<DrainableIOBuffer> request_headers_; |
| 113 |
| 114 // The request body data. |
| 115 scoped_ptr<UploadDataStream> request_body_; |
| 116 |
| 117 // Temporary buffer for reading. |
| 118 scoped_refptr<GrowableIOBuffer> read_buf_; |
| 119 |
| 120 // Offset of the first unused byte in |read_buf_|. May be nonzero due to |
| 121 // a 1xx header, or body data in the same packet as header data. |
| 122 int read_buf_unused_offset_; |
| 123 |
| 124 // The amount beyond |read_buf_unused_offset_| where the status line starts; |
| 125 // -1 if not found yet. |
| 126 int response_header_start_offset_; |
| 127 |
| 128 // The parsed response headers. |
| 129 HttpResponseInfo response_; |
| 130 |
| 131 // Indicates the content length. If this value is less than zero |
| 132 // (and chunked_decoder_ is null), then we must read until the server |
| 133 // closes the connection. |
| 134 int64 response_body_length_; |
| 135 |
| 136 // Keep track of the number of response body bytes read so far. |
| 137 int64 response_body_read_; |
| 138 |
| 139 // Helper if the data is chunked. |
| 140 scoped_ptr<HttpChunkedDecoder> chunked_decoder_; |
| 141 |
| 142 // Where the caller wants the body data. |
| 143 scoped_refptr<IOBuffer> user_read_buf_; |
| 144 int user_read_buf_len_; |
| 145 |
| 146 // The callback to notify a user that their request or response is |
| 147 // complete or there was an error |
| 148 CompletionCallback* user_callback_; |
| 149 |
| 150 // In the client callback, the client can do anything, including |
| 151 // destroying this class, so any pending callback must be issued |
| 152 // after everything else is done. When it is time to issue the client |
| 153 // callback, move it from |user_callback_| to |scheduled_callback_|. |
| 154 CompletionCallback* scheduled_callback_; |
| 155 |
| 156 // The underlying socket. |
| 157 ClientSocketHandle* const connection_; |
| 158 |
| 159 // Callback to be used when doing IO. |
| 160 CompletionCallbackImpl<HttpStreamParser> io_callback_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(HttpStreamParser); |
| 163 }; |
| 164 |
| 165 } // namespace net |
| 166 |
| 167 #endif // NET_HTTP_HTTP_STREAM_PARSER_H_ |
OLD | NEW |