OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <memory> | |
12 | |
13 #if defined(WEBRTC_WIN) | |
14 #include "webrtc/base/win32.h" | |
15 #else // !WEBRTC_WIN | |
16 #define SEC_E_CERT_EXPIRED (-2146893016) | |
17 #endif // !WEBRTC_WIN | |
18 | |
19 #include "webrtc/base/checks.h" | |
20 #include "webrtc/base/httpbase.h" | |
21 #include "webrtc/base/logging.h" | |
22 #include "webrtc/base/socket.h" | |
23 #include "webrtc/base/stringutils.h" | |
24 #include "webrtc/base/thread.h" | |
25 | |
26 namespace rtc { | |
27 | |
28 ////////////////////////////////////////////////////////////////////// | |
29 // Helpers | |
30 ////////////////////////////////////////////////////////////////////// | |
31 | |
32 bool MatchHeader(const char* str, size_t len, HttpHeader header) { | |
33 const char* const header_str = ToString(header); | |
34 const size_t header_len = strlen(header_str); | |
35 return (len == header_len) && (_strnicmp(str, header_str, header_len) == 0); | |
36 } | |
37 | |
38 enum { | |
39 MSG_READ | |
40 }; | |
41 | |
42 ////////////////////////////////////////////////////////////////////// | |
43 // HttpParser | |
44 ////////////////////////////////////////////////////////////////////// | |
45 | |
46 HttpParser::HttpParser() { | |
47 reset(); | |
48 } | |
49 | |
50 HttpParser::~HttpParser() { | |
51 } | |
52 | |
53 void | |
54 HttpParser::reset() { | |
55 state_ = ST_LEADER; | |
56 chunked_ = false; | |
57 data_size_ = SIZE_UNKNOWN; | |
58 } | |
59 | |
60 HttpParser::ProcessResult | |
61 HttpParser::Process(const char* buffer, size_t len, size_t* processed, | |
62 HttpError* error) { | |
63 *processed = 0; | |
64 *error = HE_NONE; | |
65 | |
66 if (state_ >= ST_COMPLETE) { | |
67 RTC_NOTREACHED(); | |
68 return PR_COMPLETE; | |
69 } | |
70 | |
71 while (true) { | |
72 if (state_ < ST_DATA) { | |
73 size_t pos = *processed; | |
74 while ((pos < len) && (buffer[pos] != '\n')) { | |
75 pos += 1; | |
76 } | |
77 if (pos >= len) { | |
78 break; // don't have a full header | |
79 } | |
80 const char* line = buffer + *processed; | |
81 size_t len = (pos - *processed); | |
82 *processed = pos + 1; | |
83 while ((len > 0) && isspace(static_cast<unsigned char>(line[len-1]))) { | |
84 len -= 1; | |
85 } | |
86 ProcessResult result = ProcessLine(line, len, error); | |
87 LOG(LS_VERBOSE) << "Processed line, result=" << result; | |
88 | |
89 if (PR_CONTINUE != result) { | |
90 return result; | |
91 } | |
92 } else if (data_size_ == 0) { | |
93 if (chunked_) { | |
94 state_ = ST_CHUNKTERM; | |
95 } else { | |
96 return PR_COMPLETE; | |
97 } | |
98 } else { | |
99 size_t available = len - *processed; | |
100 if (available <= 0) { | |
101 break; // no more data | |
102 } | |
103 if ((data_size_ != SIZE_UNKNOWN) && (available > data_size_)) { | |
104 available = data_size_; | |
105 } | |
106 size_t read = 0; | |
107 ProcessResult result = ProcessData(buffer + *processed, available, read, | |
108 error); | |
109 LOG(LS_VERBOSE) << "Processed data, result: " << result << " read: " | |
110 << read << " err: " << error; | |
111 | |
112 if (PR_CONTINUE != result) { | |
113 return result; | |
114 } | |
115 *processed += read; | |
116 if (data_size_ != SIZE_UNKNOWN) { | |
117 data_size_ -= read; | |
118 } | |
119 } | |
120 } | |
121 | |
122 return PR_CONTINUE; | |
123 } | |
124 | |
125 HttpParser::ProcessResult | |
126 HttpParser::ProcessLine(const char* line, size_t len, HttpError* error) { | |
127 LOG_F(LS_VERBOSE) << " state: " << state_ << " line: " | |
128 << std::string(line, len) << " len: " << len << " err: " | |
129 << error; | |
130 | |
131 switch (state_) { | |
132 case ST_LEADER: | |
133 state_ = ST_HEADERS; | |
134 return ProcessLeader(line, len, error); | |
135 | |
136 case ST_HEADERS: | |
137 if (len > 0) { | |
138 const char* value = strchrn(line, len, ':'); | |
139 if (!value) { | |
140 *error = HE_PROTOCOL; | |
141 return PR_COMPLETE; | |
142 } | |
143 size_t nlen = (value - line); | |
144 const char* eol = line + len; | |
145 do { | |
146 value += 1; | |
147 } while ((value < eol) && isspace(static_cast<unsigned char>(*value))); | |
148 size_t vlen = eol - value; | |
149 if (MatchHeader(line, nlen, HH_CONTENT_LENGTH)) { | |
150 // sscanf isn't safe with strings that aren't null-terminated, and there | |
151 // is no guarantee that |value| is. | |
152 // Create a local copy that is null-terminated. | |
153 std::string value_str(value, vlen); | |
154 unsigned int temp_size; | |
155 if (sscanf(value_str.c_str(), "%u", &temp_size) != 1) { | |
156 *error = HE_PROTOCOL; | |
157 return PR_COMPLETE; | |
158 } | |
159 data_size_ = static_cast<size_t>(temp_size); | |
160 } else if (MatchHeader(line, nlen, HH_TRANSFER_ENCODING)) { | |
161 if ((vlen == 7) && (_strnicmp(value, "chunked", 7) == 0)) { | |
162 chunked_ = true; | |
163 } else if ((vlen == 8) && (_strnicmp(value, "identity", 8) == 0)) { | |
164 chunked_ = false; | |
165 } else { | |
166 *error = HE_PROTOCOL; | |
167 return PR_COMPLETE; | |
168 } | |
169 } | |
170 return ProcessHeader(line, nlen, value, vlen, error); | |
171 } else { | |
172 state_ = chunked_ ? ST_CHUNKSIZE : ST_DATA; | |
173 return ProcessHeaderComplete(chunked_, data_size_, error); | |
174 } | |
175 break; | |
176 | |
177 case ST_CHUNKSIZE: | |
178 if (len > 0) { | |
179 char* ptr = nullptr; | |
180 data_size_ = strtoul(line, &ptr, 16); | |
181 if (ptr != line + len) { | |
182 *error = HE_PROTOCOL; | |
183 return PR_COMPLETE; | |
184 } | |
185 state_ = (data_size_ == 0) ? ST_TRAILERS : ST_DATA; | |
186 } else { | |
187 *error = HE_PROTOCOL; | |
188 return PR_COMPLETE; | |
189 } | |
190 break; | |
191 | |
192 case ST_CHUNKTERM: | |
193 if (len > 0) { | |
194 *error = HE_PROTOCOL; | |
195 return PR_COMPLETE; | |
196 } else { | |
197 state_ = chunked_ ? ST_CHUNKSIZE : ST_DATA; | |
198 } | |
199 break; | |
200 | |
201 case ST_TRAILERS: | |
202 if (len == 0) { | |
203 return PR_COMPLETE; | |
204 } | |
205 // *error = onHttpRecvTrailer(); | |
206 break; | |
207 | |
208 default: | |
209 RTC_NOTREACHED(); | |
210 break; | |
211 } | |
212 | |
213 return PR_CONTINUE; | |
214 } | |
215 | |
216 bool | |
217 HttpParser::is_valid_end_of_input() const { | |
218 return (state_ == ST_DATA) && (data_size_ == SIZE_UNKNOWN); | |
219 } | |
220 | |
221 void | |
222 HttpParser::complete(HttpError error) { | |
223 if (state_ < ST_COMPLETE) { | |
224 state_ = ST_COMPLETE; | |
225 OnComplete(error); | |
226 } | |
227 } | |
228 | |
229 ////////////////////////////////////////////////////////////////////// | |
230 // HttpBase::DocumentStream | |
231 ////////////////////////////////////////////////////////////////////// | |
232 | |
233 class BlockingMemoryStream : public ExternalMemoryStream { | |
234 public: | |
235 BlockingMemoryStream(char* buffer, size_t size) | |
236 : ExternalMemoryStream(buffer, size) { } | |
237 | |
238 StreamResult DoReserve(size_t size, int* error) override { | |
239 return (buffer_length_ >= size) ? SR_SUCCESS : SR_BLOCK; | |
240 } | |
241 }; | |
242 | |
243 class HttpBase::DocumentStream : public StreamInterface { | |
244 public: | |
245 DocumentStream(HttpBase* base) : base_(base), error_(HE_DEFAULT) { } | |
246 | |
247 StreamState GetState() const override { | |
248 if (nullptr == base_) | |
249 return SS_CLOSED; | |
250 if (HM_RECV == base_->mode_) | |
251 return SS_OPEN; | |
252 return SS_OPENING; | |
253 } | |
254 | |
255 StreamResult Read(void* buffer, | |
256 size_t buffer_len, | |
257 size_t* read, | |
258 int* error) override { | |
259 if (!base_) { | |
260 if (error) *error = error_; | |
261 return (HE_NONE == error_) ? SR_EOS : SR_ERROR; | |
262 } | |
263 | |
264 if (HM_RECV != base_->mode_) { | |
265 return SR_BLOCK; | |
266 } | |
267 | |
268 // DoReceiveLoop writes http document data to the StreamInterface* document | |
269 // member of HttpData. In this case, we want this data to be written | |
270 // directly to our buffer. To accomplish this, we wrap our buffer with a | |
271 // StreamInterface, and replace the existing document with our wrapper. | |
272 // When the method returns, we restore the old document. Ideally, we would | |
273 // pass our StreamInterface* to DoReceiveLoop, but due to the callbacks | |
274 // of HttpParser, we would still need to store the pointer temporarily. | |
275 std::unique_ptr<StreamInterface> stream( | |
276 new BlockingMemoryStream(reinterpret_cast<char*>(buffer), buffer_len)); | |
277 | |
278 // Replace the existing document with our wrapped buffer. | |
279 base_->data_->document.swap(stream); | |
280 | |
281 // Pump the I/O loop. DoReceiveLoop is guaranteed not to attempt to | |
282 // complete the I/O process, which means that our wrapper is not in danger | |
283 // of being deleted. To ensure this, DoReceiveLoop returns true when it | |
284 // wants complete to be called. We make sure to uninstall our wrapper | |
285 // before calling complete(). | |
286 HttpError http_error; | |
287 bool complete = base_->DoReceiveLoop(&http_error); | |
288 | |
289 // Reinstall the original output document. | |
290 base_->data_->document.swap(stream); | |
291 | |
292 // If we reach the end of the receive stream, we disconnect our stream | |
293 // adapter from the HttpBase, and further calls to read will either return | |
294 // EOS or ERROR, appropriately. Finally, we call complete(). | |
295 StreamResult result = SR_BLOCK; | |
296 if (complete) { | |
297 HttpBase* base = Disconnect(http_error); | |
298 if (error) *error = error_; | |
299 result = (HE_NONE == error_) ? SR_EOS : SR_ERROR; | |
300 base->complete(http_error); | |
301 } | |
302 | |
303 // Even if we are complete, if some data was read we must return SUCCESS. | |
304 // Future Reads will return EOS or ERROR based on the error_ variable. | |
305 size_t position; | |
306 stream->GetPosition(&position); | |
307 if (position > 0) { | |
308 if (read) *read = position; | |
309 result = SR_SUCCESS; | |
310 } | |
311 return result; | |
312 } | |
313 | |
314 StreamResult Write(const void* data, | |
315 size_t data_len, | |
316 size_t* written, | |
317 int* error) override { | |
318 if (error) *error = -1; | |
319 return SR_ERROR; | |
320 } | |
321 | |
322 void Close() override { | |
323 if (base_) { | |
324 HttpBase* base = Disconnect(HE_NONE); | |
325 if (HM_RECV == base->mode_ && base->http_stream_) { | |
326 // Read I/O could have been stalled on the user of this DocumentStream, | |
327 // so restart the I/O process now that we've removed ourselves. | |
328 base->http_stream_->PostEvent(SE_READ, 0); | |
329 } | |
330 } | |
331 } | |
332 | |
333 bool GetAvailable(size_t* size) const override { | |
334 if (!base_ || HM_RECV != base_->mode_) | |
335 return false; | |
336 size_t data_size = base_->GetDataRemaining(); | |
337 if (SIZE_UNKNOWN == data_size) | |
338 return false; | |
339 if (size) | |
340 *size = data_size; | |
341 return true; | |
342 } | |
343 | |
344 HttpBase* Disconnect(HttpError error) { | |
345 RTC_DCHECK(nullptr != base_); | |
346 RTC_DCHECK(nullptr != base_->doc_stream_); | |
347 HttpBase* base = base_; | |
348 base_->doc_stream_ = nullptr; | |
349 base_ = nullptr; | |
350 error_ = error; | |
351 return base; | |
352 } | |
353 | |
354 private: | |
355 HttpBase* base_; | |
356 HttpError error_; | |
357 }; | |
358 | |
359 ////////////////////////////////////////////////////////////////////// | |
360 // HttpBase | |
361 ////////////////////////////////////////////////////////////////////// | |
362 | |
363 HttpBase::HttpBase() | |
364 : mode_(HM_NONE), | |
365 data_(nullptr), | |
366 notify_(nullptr), | |
367 http_stream_(nullptr), | |
368 doc_stream_(nullptr) {} | |
369 | |
370 HttpBase::~HttpBase() { | |
371 RTC_DCHECK(HM_NONE == mode_); | |
372 } | |
373 | |
374 bool | |
375 HttpBase::isConnected() const { | |
376 return (http_stream_ != nullptr) && (http_stream_->GetState() == SS_OPEN); | |
377 } | |
378 | |
379 bool | |
380 HttpBase::attach(StreamInterface* stream) { | |
381 if ((mode_ != HM_NONE) || (http_stream_ != nullptr) || (stream == nullptr)) { | |
382 RTC_NOTREACHED(); | |
383 return false; | |
384 } | |
385 http_stream_ = stream; | |
386 http_stream_->SignalEvent.connect(this, &HttpBase::OnHttpStreamEvent); | |
387 mode_ = (http_stream_->GetState() == SS_OPENING) ? HM_CONNECT : HM_NONE; | |
388 return true; | |
389 } | |
390 | |
391 StreamInterface* | |
392 HttpBase::detach() { | |
393 RTC_DCHECK(HM_NONE == mode_); | |
394 if (mode_ != HM_NONE) { | |
395 return nullptr; | |
396 } | |
397 StreamInterface* stream = http_stream_; | |
398 http_stream_ = nullptr; | |
399 if (stream) { | |
400 stream->SignalEvent.disconnect(this); | |
401 } | |
402 return stream; | |
403 } | |
404 | |
405 void | |
406 HttpBase::send(HttpData* data) { | |
407 RTC_DCHECK(HM_NONE == mode_); | |
408 if (mode_ != HM_NONE) { | |
409 return; | |
410 } else if (!isConnected()) { | |
411 OnHttpStreamEvent(http_stream_, SE_CLOSE, HE_DISCONNECTED); | |
412 return; | |
413 } | |
414 | |
415 mode_ = HM_SEND; | |
416 data_ = data; | |
417 len_ = 0; | |
418 ignore_data_ = chunk_data_ = false; | |
419 | |
420 if (data_->document) { | |
421 data_->document->SignalEvent.connect(this, &HttpBase::OnDocumentEvent); | |
422 } | |
423 | |
424 std::string encoding; | |
425 if (data_->hasHeader(HH_TRANSFER_ENCODING, &encoding) | |
426 && (encoding == "chunked")) { | |
427 chunk_data_ = true; | |
428 } | |
429 | |
430 len_ = data_->formatLeader(buffer_, sizeof(buffer_)); | |
431 len_ += strcpyn(buffer_ + len_, sizeof(buffer_) - len_, "\r\n"); | |
432 | |
433 header_ = data_->begin(); | |
434 if (header_ == data_->end()) { | |
435 // We must call this at least once, in the case where there are no headers. | |
436 queue_headers(); | |
437 } | |
438 | |
439 flush_data(); | |
440 } | |
441 | |
442 void | |
443 HttpBase::recv(HttpData* data) { | |
444 RTC_DCHECK(HM_NONE == mode_); | |
445 if (mode_ != HM_NONE) { | |
446 return; | |
447 } else if (!isConnected()) { | |
448 OnHttpStreamEvent(http_stream_, SE_CLOSE, HE_DISCONNECTED); | |
449 return; | |
450 } | |
451 | |
452 mode_ = HM_RECV; | |
453 data_ = data; | |
454 len_ = 0; | |
455 ignore_data_ = chunk_data_ = false; | |
456 | |
457 reset(); | |
458 if (doc_stream_) { | |
459 doc_stream_->SignalEvent(doc_stream_, SE_OPEN | SE_READ, 0); | |
460 } else { | |
461 read_and_process_data(); | |
462 } | |
463 } | |
464 | |
465 void | |
466 HttpBase::abort(HttpError err) { | |
467 if (mode_ != HM_NONE) { | |
468 if (http_stream_ != nullptr) { | |
469 http_stream_->Close(); | |
470 } | |
471 do_complete(err); | |
472 } | |
473 } | |
474 | |
475 StreamInterface* HttpBase::GetDocumentStream() { | |
476 if (doc_stream_) | |
477 return nullptr; | |
478 doc_stream_ = new DocumentStream(this); | |
479 return doc_stream_; | |
480 } | |
481 | |
482 HttpError HttpBase::HandleStreamClose(int error) { | |
483 if (http_stream_ != nullptr) { | |
484 http_stream_->Close(); | |
485 } | |
486 if (error == 0) { | |
487 if ((mode_ == HM_RECV) && is_valid_end_of_input()) { | |
488 return HE_NONE; | |
489 } else { | |
490 return HE_DISCONNECTED; | |
491 } | |
492 } else if (error == SOCKET_EACCES) { | |
493 return HE_AUTH; | |
494 } else if (error == SEC_E_CERT_EXPIRED) { | |
495 return HE_CERTIFICATE_EXPIRED; | |
496 } | |
497 LOG_F(LS_ERROR) << "(" << error << ")"; | |
498 return (HM_CONNECT == mode_) ? HE_CONNECT_FAILED : HE_SOCKET_ERROR; | |
499 } | |
500 | |
501 bool HttpBase::DoReceiveLoop(HttpError* error) { | |
502 RTC_DCHECK(HM_RECV == mode_); | |
503 RTC_DCHECK(nullptr != error); | |
504 | |
505 // Do to the latency between receiving read notifications from | |
506 // pseudotcpchannel, we rely on repeated calls to read in order to acheive | |
507 // ideal throughput. The number of reads is limited to prevent starving | |
508 // the caller. | |
509 | |
510 size_t loop_count = 0; | |
511 const size_t kMaxReadCount = 20; | |
512 bool process_requires_more_data = false; | |
513 do { | |
514 // The most frequent use of this function is response to new data available | |
515 // on http_stream_. Therefore, we optimize by attempting to read from the | |
516 // network first (as opposed to processing existing data first). | |
517 | |
518 if (len_ < sizeof(buffer_)) { | |
519 // Attempt to buffer more data. | |
520 size_t read; | |
521 int read_error; | |
522 StreamResult read_result = http_stream_->Read(buffer_ + len_, | |
523 sizeof(buffer_) - len_, | |
524 &read, &read_error); | |
525 switch (read_result) { | |
526 case SR_SUCCESS: | |
527 RTC_DCHECK(len_ + read <= sizeof(buffer_)); | |
528 len_ += read; | |
529 break; | |
530 case SR_BLOCK: | |
531 if (process_requires_more_data) { | |
532 // We're can't make progress until more data is available. | |
533 return false; | |
534 } | |
535 // Attempt to process the data already in our buffer. | |
536 break; | |
537 case SR_EOS: | |
538 // Clean close, with no error. | |
539 read_error = 0; | |
540 FALLTHROUGH(); // Fall through to HandleStreamClose. | |
541 case SR_ERROR: | |
542 *error = HandleStreamClose(read_error); | |
543 return true; | |
544 } | |
545 } else if (process_requires_more_data) { | |
546 // We have too much unprocessed data in our buffer. This should only | |
547 // occur when a single HTTP header is longer than the buffer size (32K). | |
548 // Anything longer than that is almost certainly an error. | |
549 *error = HE_OVERFLOW; | |
550 return true; | |
551 } | |
552 | |
553 // Process data in our buffer. Process is not guaranteed to process all | |
554 // the buffered data. In particular, it will wait until a complete | |
555 // protocol element (such as http header, or chunk size) is available, | |
556 // before processing it in its entirety. Also, it is valid and sometimes | |
557 // necessary to call Process with an empty buffer, since the state machine | |
558 // may have interrupted state transitions to complete. | |
559 size_t processed; | |
560 ProcessResult process_result = Process(buffer_, len_, &processed, | |
561 error); | |
562 RTC_DCHECK(processed <= len_); | |
563 len_ -= processed; | |
564 memmove(buffer_, buffer_ + processed, len_); | |
565 switch (process_result) { | |
566 case PR_CONTINUE: | |
567 // We need more data to make progress. | |
568 process_requires_more_data = true; | |
569 break; | |
570 case PR_BLOCK: | |
571 // We're stalled on writing the processed data. | |
572 return false; | |
573 case PR_COMPLETE: | |
574 // *error already contains the correct code. | |
575 return true; | |
576 } | |
577 } while (++loop_count <= kMaxReadCount); | |
578 | |
579 LOG_F(LS_WARNING) << "danger of starvation"; | |
580 return false; | |
581 } | |
582 | |
583 void | |
584 HttpBase::read_and_process_data() { | |
585 HttpError error; | |
586 if (DoReceiveLoop(&error)) { | |
587 complete(error); | |
588 } | |
589 } | |
590 | |
591 void | |
592 HttpBase::flush_data() { | |
593 RTC_DCHECK(HM_SEND == mode_); | |
594 | |
595 // When send_required is true, no more buffering can occur without a network | |
596 // write. | |
597 bool send_required = (len_ >= sizeof(buffer_)); | |
598 | |
599 while (true) { | |
600 RTC_DCHECK(len_ <= sizeof(buffer_)); | |
601 | |
602 // HTTP is inherently sensitive to round trip latency, since a frequent use | |
603 // case is for small requests and responses to be sent back and forth, and | |
604 // the lack of pipelining forces a single request to take a minimum of the | |
605 // round trip time. As a result, it is to our benefit to pack as much data | |
606 // into each packet as possible. Thus, we defer network writes until we've | |
607 // buffered as much data as possible. | |
608 | |
609 if (!send_required && (header_ != data_->end())) { | |
610 // First, attempt to queue more header data. | |
611 send_required = queue_headers(); | |
612 } | |
613 | |
614 if (!send_required && data_->document) { | |
615 // Next, attempt to queue document data. | |
616 | |
617 const size_t kChunkDigits = 8; | |
618 size_t offset, reserve; | |
619 if (chunk_data_) { | |
620 // Reserve characters at the start for X-byte hex value and \r\n | |
621 offset = len_ + kChunkDigits + 2; | |
622 // ... and 2 characters at the end for \r\n | |
623 reserve = offset + 2; | |
624 } else { | |
625 offset = len_; | |
626 reserve = offset; | |
627 } | |
628 | |
629 if (reserve >= sizeof(buffer_)) { | |
630 send_required = true; | |
631 } else { | |
632 size_t read; | |
633 int error; | |
634 StreamResult result = data_->document->Read(buffer_ + offset, | |
635 sizeof(buffer_) - reserve, | |
636 &read, &error); | |
637 if (result == SR_SUCCESS) { | |
638 RTC_DCHECK(reserve + read <= sizeof(buffer_)); | |
639 if (chunk_data_) { | |
640 // Prepend the chunk length in hex. | |
641 // Note: sprintfn appends a null terminator, which is why we can't | |
642 // combine it with the line terminator. | |
643 sprintfn(buffer_ + len_, kChunkDigits + 1, "%.*x", | |
644 kChunkDigits, read); | |
645 // Add line terminator to the chunk length. | |
646 memcpy(buffer_ + len_ + kChunkDigits, "\r\n", 2); | |
647 // Add line terminator to the end of the chunk. | |
648 memcpy(buffer_ + offset + read, "\r\n", 2); | |
649 } | |
650 len_ = reserve + read; | |
651 } else if (result == SR_BLOCK) { | |
652 // Nothing to do but flush data to the network. | |
653 send_required = true; | |
654 } else if (result == SR_EOS) { | |
655 if (chunk_data_) { | |
656 // Append the empty chunk and empty trailers, then turn off | |
657 // chunking. | |
658 RTC_DCHECK(len_ + 5 <= sizeof(buffer_)); | |
659 memcpy(buffer_ + len_, "0\r\n\r\n", 5); | |
660 len_ += 5; | |
661 chunk_data_ = false; | |
662 } else if (0 == len_) { | |
663 // No more data to read, and no more data to write. | |
664 do_complete(); | |
665 return; | |
666 } | |
667 // Although we are done reading data, there is still data which needs | |
668 // to be flushed to the network. | |
669 send_required = true; | |
670 } else { | |
671 LOG_F(LS_ERROR) << "Read error: " << error; | |
672 do_complete(HE_STREAM); | |
673 return; | |
674 } | |
675 } | |
676 } | |
677 | |
678 if (0 == len_) { | |
679 // No data currently available to send. | |
680 if (!data_->document) { | |
681 // If there is no source document, that means we're done. | |
682 do_complete(); | |
683 } | |
684 return; | |
685 } | |
686 | |
687 size_t written; | |
688 int error; | |
689 StreamResult result = http_stream_->Write(buffer_, len_, &written, &error); | |
690 if (result == SR_SUCCESS) { | |
691 RTC_DCHECK(written <= len_); | |
692 len_ -= written; | |
693 memmove(buffer_, buffer_ + written, len_); | |
694 send_required = false; | |
695 } else if (result == SR_BLOCK) { | |
696 if (send_required) { | |
697 // Nothing more we can do until network is writeable. | |
698 return; | |
699 } | |
700 } else { | |
701 RTC_DCHECK(result == SR_ERROR); | |
702 LOG_F(LS_ERROR) << "error"; | |
703 OnHttpStreamEvent(http_stream_, SE_CLOSE, error); | |
704 return; | |
705 } | |
706 } | |
707 | |
708 RTC_NOTREACHED(); | |
709 } | |
710 | |
711 bool | |
712 HttpBase::queue_headers() { | |
713 RTC_DCHECK(HM_SEND == mode_); | |
714 while (header_ != data_->end()) { | |
715 size_t len = sprintfn(buffer_ + len_, sizeof(buffer_) - len_, | |
716 "%.*s: %.*s\r\n", | |
717 header_->first.size(), header_->first.data(), | |
718 header_->second.size(), header_->second.data()); | |
719 if (len_ + len < sizeof(buffer_) - 3) { | |
720 len_ += len; | |
721 ++header_; | |
722 } else if (len_ == 0) { | |
723 LOG(WARNING) << "discarding header that is too long: " << header_->first; | |
724 ++header_; | |
725 } else { | |
726 // Not enough room for the next header, write to network first. | |
727 return true; | |
728 } | |
729 } | |
730 // End of headers | |
731 len_ += strcpyn(buffer_ + len_, sizeof(buffer_) - len_, "\r\n"); | |
732 return false; | |
733 } | |
734 | |
735 void | |
736 HttpBase::do_complete(HttpError err) { | |
737 RTC_DCHECK(mode_ != HM_NONE); | |
738 HttpMode mode = mode_; | |
739 mode_ = HM_NONE; | |
740 if (data_ && data_->document) { | |
741 data_->document->SignalEvent.disconnect(this); | |
742 } | |
743 data_ = nullptr; | |
744 if ((HM_RECV == mode) && doc_stream_) { | |
745 RTC_DCHECK(HE_NONE != | |
746 err); // We should have Disconnected doc_stream_ already. | |
747 DocumentStream* ds = doc_stream_; | |
748 ds->Disconnect(err); | |
749 ds->SignalEvent(ds, SE_CLOSE, err); | |
750 } | |
751 if (notify_) { | |
752 notify_->onHttpComplete(mode, err); | |
753 } | |
754 } | |
755 | |
756 // | |
757 // Stream Signals | |
758 // | |
759 | |
760 void | |
761 HttpBase::OnHttpStreamEvent(StreamInterface* stream, int events, int error) { | |
762 RTC_DCHECK(stream == http_stream_); | |
763 if ((events & SE_OPEN) && (mode_ == HM_CONNECT)) { | |
764 do_complete(); | |
765 return; | |
766 } | |
767 | |
768 if ((events & SE_WRITE) && (mode_ == HM_SEND)) { | |
769 flush_data(); | |
770 return; | |
771 } | |
772 | |
773 if ((events & SE_READ) && (mode_ == HM_RECV)) { | |
774 if (doc_stream_) { | |
775 doc_stream_->SignalEvent(doc_stream_, SE_READ, 0); | |
776 } else { | |
777 read_and_process_data(); | |
778 } | |
779 return; | |
780 } | |
781 | |
782 if ((events & SE_CLOSE) == 0) | |
783 return; | |
784 | |
785 HttpError http_error = HandleStreamClose(error); | |
786 if (mode_ == HM_RECV) { | |
787 complete(http_error); | |
788 } else if (mode_ != HM_NONE) { | |
789 do_complete(http_error); | |
790 } else if (notify_) { | |
791 notify_->onHttpClosed(http_error); | |
792 } | |
793 } | |
794 | |
795 void | |
796 HttpBase::OnDocumentEvent(StreamInterface* stream, int events, int error) { | |
797 RTC_DCHECK(stream == data_->document.get()); | |
798 if ((events & SE_WRITE) && (mode_ == HM_RECV)) { | |
799 read_and_process_data(); | |
800 return; | |
801 } | |
802 | |
803 if ((events & SE_READ) && (mode_ == HM_SEND)) { | |
804 flush_data(); | |
805 return; | |
806 } | |
807 | |
808 if (events & SE_CLOSE) { | |
809 LOG_F(LS_ERROR) << "Read error: " << error; | |
810 do_complete(HE_STREAM); | |
811 return; | |
812 } | |
813 } | |
814 | |
815 // | |
816 // HttpParser Implementation | |
817 // | |
818 | |
819 HttpParser::ProcessResult | |
820 HttpBase::ProcessLeader(const char* line, size_t len, HttpError* error) { | |
821 *error = data_->parseLeader(line, len); | |
822 return (HE_NONE == *error) ? PR_CONTINUE : PR_COMPLETE; | |
823 } | |
824 | |
825 HttpParser::ProcessResult | |
826 HttpBase::ProcessHeader(const char* name, size_t nlen, const char* value, | |
827 size_t vlen, HttpError* error) { | |
828 std::string sname(name, nlen), svalue(value, vlen); | |
829 data_->addHeader(sname, svalue); | |
830 return PR_CONTINUE; | |
831 } | |
832 | |
833 HttpParser::ProcessResult | |
834 HttpBase::ProcessHeaderComplete(bool chunked, size_t& data_size, | |
835 HttpError* error) { | |
836 StreamInterface* old_docstream = doc_stream_; | |
837 if (notify_) { | |
838 *error = notify_->onHttpHeaderComplete(chunked, data_size); | |
839 // The request must not be aborted as a result of this callback. | |
840 RTC_DCHECK(nullptr != data_); | |
841 } | |
842 if ((HE_NONE == *error) && data_->document) { | |
843 data_->document->SignalEvent.connect(this, &HttpBase::OnDocumentEvent); | |
844 } | |
845 if (HE_NONE != *error) { | |
846 return PR_COMPLETE; | |
847 } | |
848 if (old_docstream != doc_stream_) { | |
849 // Break out of Process loop, since our I/O model just changed. | |
850 return PR_BLOCK; | |
851 } | |
852 return PR_CONTINUE; | |
853 } | |
854 | |
855 HttpParser::ProcessResult | |
856 HttpBase::ProcessData(const char* data, size_t len, size_t& read, | |
857 HttpError* error) { | |
858 if (ignore_data_ || !data_->document) { | |
859 read = len; | |
860 return PR_CONTINUE; | |
861 } | |
862 int write_error = 0; | |
863 switch (data_->document->Write(data, len, &read, &write_error)) { | |
864 case SR_SUCCESS: | |
865 return PR_CONTINUE; | |
866 case SR_BLOCK: | |
867 return PR_BLOCK; | |
868 case SR_EOS: | |
869 LOG_F(LS_ERROR) << "Unexpected EOS"; | |
870 *error = HE_STREAM; | |
871 return PR_COMPLETE; | |
872 case SR_ERROR: | |
873 default: | |
874 LOG_F(LS_ERROR) << "Write error: " << write_error; | |
875 *error = HE_STREAM; | |
876 return PR_COMPLETE; | |
877 } | |
878 } | |
879 | |
880 void | |
881 HttpBase::OnComplete(HttpError err) { | |
882 LOG_F(LS_VERBOSE); | |
883 do_complete(err); | |
884 } | |
885 | |
886 } // namespace rtc | |
OLD | NEW |