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