| 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" |  | 
| 10 | 9 | 
| 11 namespace net { | 10 namespace net { | 
| 12 namespace test_server { | 11 namespace test_server { | 
| 13 | 12 | 
| 14 HttpConnection::HttpConnection(scoped_ptr<StreamSocket> socket, | 13 HttpConnection::HttpConnection(scoped_ptr<StreamSocket> socket, | 
| 15                                const HandleRequestCallback& callback) | 14                                const HandleRequestCallback& callback) | 
| 16     : socket_(socket.Pass()), | 15     : socket_(socket.Pass()), | 
| 17       callback_(callback), | 16       callback_(callback), | 
| 18       read_buf_(new IOBufferWithSize(4096)) {} | 17       read_buf_(new IOBufferWithSize(4096)) {} | 
| 19 | 18 | 
| 20 HttpConnection::~HttpConnection() { | 19 HttpConnection::~HttpConnection() { | 
| 21 } | 20 } | 
| 22 | 21 | 
| 23 void HttpConnection::SendResponse(scoped_ptr<HttpResponse> response, | 22 void HttpConnection::SendResponseBytes(const std::string& response_string, | 
| 24                                   const base::Closure& callback) { | 23                                        SendCompleteCallback callback) { | 
| 25   const std::string response_string = response->ToResponseString(); |  | 
| 26   if (response_string.length() > 0) { | 24   if (response_string.length() > 0) { | 
| 27     scoped_refptr<DrainableIOBuffer> write_buf(new DrainableIOBuffer( | 25     scoped_refptr<DrainableIOBuffer> write_buf(new DrainableIOBuffer( | 
| 28         new StringIOBuffer(response_string), response_string.length())); | 26         new StringIOBuffer(response_string), response_string.length())); | 
| 29 | 27 | 
| 30     SendInternal(callback, write_buf); | 28     SendInternal(callback, write_buf); | 
| 31   } else { | 29   } else { | 
| 32     callback.Run(); | 30     callback.Run(); | 
| 33   } | 31   } | 
| 34 } | 32 } | 
| 35 | 33 | 
| 36 void HttpConnection::SendInternal(const base::Closure& callback, | 34 void HttpConnection::SendInternal(const base::Closure& callback, | 
| 37                                   scoped_refptr<DrainableIOBuffer> buf) { | 35                                   scoped_refptr<DrainableIOBuffer> buf) { | 
| 38   while (buf->BytesRemaining() > 0) { | 36   while (buf->BytesRemaining() > 0) { | 
| 39     int rv = socket_->Write(buf.get(), buf->BytesRemaining(), | 37     int rv = socket_->Write(buf.get(), buf->BytesRemaining(), | 
| 40                             base::Bind(&HttpConnection::OnSendInternalDone, | 38                             base::Bind(&HttpConnection::OnSendInternalDone, | 
| 41                                        base::Unretained(this), callback, buf)); | 39                                        base::Unretained(this), callback, buf)); | 
| 42     if (rv == ERR_IO_PENDING) | 40     if (rv == ERR_IO_PENDING) | 
| 43       return; | 41       return; | 
| 44 | 42 | 
|  | 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   } | 
| 56   buf->DidConsume(rv); | 60   buf->DidConsume(rv); | 
| 57   SendInternal(callback, buf); | 61   SendInternal(callback, buf); | 
| 58 } | 62 } | 
| 59 | 63 | 
| 60 int HttpConnection::ReadData(const CompletionCallback& callback) { | 64 int HttpConnection::ReadData(const CompletionCallback& callback) { | 
| 61   return socket_->Read(read_buf_.get(), read_buf_->size(), callback); | 65   return socket_->Read(read_buf_.get(), read_buf_->size(), callback); | 
| 62 } | 66 } | 
| 63 | 67 | 
| 64 bool HttpConnection::ConsumeData(int size) { | 68 bool HttpConnection::ConsumeData(int size) { | 
| 65   request_parser_.ProcessChunk(base::StringPiece(read_buf_->data(), size)); | 69   request_parser_.ProcessChunk(base::StringPiece(read_buf_->data(), size)); | 
| 66   if (request_parser_.ParseRequest() == HttpRequestParser::ACCEPTED) { | 70   if (request_parser_.ParseRequest() == HttpRequestParser::ACCEPTED) { | 
| 67     callback_.Run(this, request_parser_.GetRequest()); | 71     callback_.Run(this, request_parser_.GetRequest()); | 
| 68     return true; | 72     return true; | 
| 69   } | 73   } | 
| 70   return false; | 74   return false; | 
| 71 } | 75 } | 
| 72 | 76 | 
| 73 }  // namespace test_server | 77 }  // namespace test_server | 
| 74 }  // namespace net | 78 }  // namespace net | 
| OLD | NEW | 
|---|