| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "net/test/embedded_test_server/http_connection.h" | 5 #include "net/test/embedded_test_server/http_connection.h" | 
| 6 | 6 | 
| 7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" | 
| 8 #include "net/socket/stream_socket.h" | 8 #include "net/socket/stream_socket.h" | 
|  | 9 #include "net/test/embedded_test_server/http_response.h" | 
| 9 | 10 | 
| 10 namespace net { | 11 namespace net { | 
| 11 namespace test_server { | 12 namespace test_server { | 
| 12 | 13 | 
| 13 HttpConnection::HttpConnection(scoped_ptr<StreamSocket> socket, | 14 HttpConnection::HttpConnection(scoped_ptr<StreamSocket> socket, | 
| 14                                const HandleRequestCallback& callback) | 15                                const HandleRequestCallback& callback) | 
| 15     : socket_(socket.Pass()), | 16     : socket_(socket.Pass()), | 
| 16       callback_(callback), | 17       callback_(callback), | 
| 17       read_buf_(new IOBufferWithSize(4096)) {} | 18       read_buf_(new IOBufferWithSize(4096)) {} | 
| 18 | 19 | 
| 19 HttpConnection::~HttpConnection() { | 20 HttpConnection::~HttpConnection() { | 
| 20 } | 21 } | 
| 21 | 22 | 
| 22 void HttpConnection::SendResponseBytes(const std::string& response_string, | 23 void HttpConnection::SendResponse(scoped_ptr<HttpResponse> response, | 
| 23                                        const SendCompleteCallback& callback) { | 24                                   const base::Closure& callback) { | 
|  | 25   const std::string response_string = response->ToResponseString(); | 
| 24   if (response_string.length() > 0) { | 26   if (response_string.length() > 0) { | 
| 25     scoped_refptr<DrainableIOBuffer> write_buf(new DrainableIOBuffer( | 27     scoped_refptr<DrainableIOBuffer> write_buf(new DrainableIOBuffer( | 
| 26         new StringIOBuffer(response_string), response_string.length())); | 28         new StringIOBuffer(response_string), response_string.length())); | 
| 27 | 29 | 
| 28     SendInternal(callback, write_buf); | 30     SendInternal(callback, write_buf); | 
| 29   } else { | 31   } else { | 
| 30     callback.Run(); | 32     callback.Run(); | 
| 31   } | 33   } | 
| 32 } | 34 } | 
| 33 | 35 | 
| 34 void HttpConnection::SendInternal(const base::Closure& callback, | 36 void HttpConnection::SendInternal(const base::Closure& callback, | 
| 35                                   scoped_refptr<DrainableIOBuffer> buf) { | 37                                   scoped_refptr<DrainableIOBuffer> buf) { | 
| 36   while (buf->BytesRemaining() > 0) { | 38   while (buf->BytesRemaining() > 0) { | 
| 37     int rv = socket_->Write(buf.get(), buf->BytesRemaining(), | 39     int rv = socket_->Write(buf.get(), buf->BytesRemaining(), | 
| 38                             base::Bind(&HttpConnection::OnSendInternalDone, | 40                             base::Bind(&HttpConnection::OnSendInternalDone, | 
| 39                                        base::Unretained(this), callback, buf)); | 41                                        base::Unretained(this), callback, buf)); | 
| 40     if (rv == ERR_IO_PENDING) | 42     if (rv == ERR_IO_PENDING) | 
| 41       return; | 43       return; | 
| 42 | 44 | 
| 43     if (rv < 0) |  | 
| 44       break; |  | 
| 45     buf->DidConsume(rv); | 45     buf->DidConsume(rv); | 
| 46   } | 46   } | 
| 47 | 47 | 
| 48   // The HttpConnection will be deleted by the callback since we only need to | 48   // The HttpConnection will be deleted by the callback since we only need to | 
| 49   // serve a single request. | 49   // serve a single request. | 
| 50   callback.Run(); | 50   callback.Run(); | 
| 51 } | 51 } | 
| 52 | 52 | 
| 53 void HttpConnection::OnSendInternalDone(const base::Closure& callback, | 53 void HttpConnection::OnSendInternalDone(const base::Closure& callback, | 
| 54                                         scoped_refptr<DrainableIOBuffer> buf, | 54                                         scoped_refptr<DrainableIOBuffer> buf, | 
| 55                                         int rv) { | 55                                         int rv) { | 
| 56   if (rv < 0) { |  | 
| 57     callback.Run(); |  | 
| 58     return; |  | 
| 59   } |  | 
| 60   buf->DidConsume(rv); | 56   buf->DidConsume(rv); | 
| 61   SendInternal(callback, buf); | 57   SendInternal(callback, buf); | 
| 62 } | 58 } | 
| 63 | 59 | 
| 64 int HttpConnection::ReadData(const CompletionCallback& callback) { | 60 int HttpConnection::ReadData(const CompletionCallback& callback) { | 
| 65   return socket_->Read(read_buf_.get(), read_buf_->size(), callback); | 61   return socket_->Read(read_buf_.get(), read_buf_->size(), callback); | 
| 66 } | 62 } | 
| 67 | 63 | 
| 68 bool HttpConnection::ConsumeData(int size) { | 64 bool HttpConnection::ConsumeData(int size) { | 
| 69   request_parser_.ProcessChunk(base::StringPiece(read_buf_->data(), size)); | 65   request_parser_.ProcessChunk(base::StringPiece(read_buf_->data(), size)); | 
| 70   if (request_parser_.ParseRequest() == HttpRequestParser::ACCEPTED) { | 66   if (request_parser_.ParseRequest() == HttpRequestParser::ACCEPTED) { | 
| 71     callback_.Run(this, request_parser_.GetRequest()); | 67     callback_.Run(this, request_parser_.GetRequest()); | 
| 72     return true; | 68     return true; | 
| 73   } | 69   } | 
| 74   return false; | 70   return false; | 
| 75 } | 71 } | 
| 76 | 72 | 
| 77 }  // namespace test_server | 73 }  // namespace test_server | 
| 78 }  // namespace net | 74 }  // namespace net | 
| OLD | NEW | 
|---|