Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: net/server/http_connection.cc

Issue 19637005: Allow HttpServer response to include custom headers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/server/http_connection.h" 5 #include "net/server/http_connection.h"
6 6
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/server/http_server.h" 7 #include "net/server/http_server.h"
8 #include "net/server/http_server_response_info.h"
10 #include "net/server/web_socket.h" 9 #include "net/server/web_socket.h"
11 #include "net/socket/stream_listen_socket.h" 10 #include "net/socket/stream_listen_socket.h"
12 11
13 namespace net { 12 namespace net {
14 13
15 int HttpConnection::last_id_ = 0; 14 int HttpConnection::last_id_ = 0;
16 15
17 void HttpConnection::Send(const std::string& data) { 16 void HttpConnection::Send(const std::string& data) {
18 if (!socket_.get()) 17 if (!socket_.get())
19 return; 18 return;
20 socket_->Send(data); 19 socket_->Send(data);
21 } 20 }
22 21
23 void HttpConnection::Send(const char* bytes, int len) { 22 void HttpConnection::Send(const char* bytes, int len) {
24 if (!socket_.get()) 23 if (!socket_.get())
25 return; 24 return;
26 socket_->Send(bytes, len); 25 socket_->Send(bytes, len);
27 } 26 }
28 27
29 void HttpConnection::Send(HttpStatusCode status_code, 28 void HttpConnection::Send(const HttpServerResponseInfo& response) {
30 const std::string& data, 29 Send(response.Serialize());
31 const std::string& content_type) {
32 if (!socket_.get())
33 return;
34
35 std::string status_message;
36 switch (status_code) {
37 case HTTP_OK:
38 status_message = "OK";
39 break;
40 case HTTP_NOT_FOUND:
41 status_message = "Not Found";
42 break;
43 case HTTP_INTERNAL_SERVER_ERROR:
44 status_message = "Internal Error";
45 break;
46 default:
47 status_message = "";
48 break;
49 }
50
51 socket_->Send(base::StringPrintf(
52 "HTTP/1.1 %d %s\r\n"
53 "Content-Type:%s\r\n"
54 "Content-Length:%d\r\n"
55 "\r\n",
56 status_code,
57 status_message.c_str(),
58 content_type.c_str(),
59 static_cast<int>(data.length())));
60 socket_->Send(data);
61 } 30 }
62 31
63 HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock) 32 HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock)
64 : server_(server), 33 : server_(server),
65 socket_(sock) { 34 socket_(sock) {
66 id_ = last_id_++; 35 id_ = last_id_++;
67 } 36 }
68 37
69 HttpConnection::~HttpConnection() { 38 HttpConnection::~HttpConnection() {
70 DetachSocket(); 39 DetachSocket();
71 server_->delegate_->OnClose(id_); 40 server_->delegate_->OnClose(id_);
72 } 41 }
73 42
74 void HttpConnection::DetachSocket() { 43 void HttpConnection::DetachSocket() {
75 socket_ = NULL; 44 socket_ = NULL;
76 } 45 }
77 46
78 void HttpConnection::Shift(int num_bytes) { 47 void HttpConnection::Shift(int num_bytes) {
79 recv_data_ = recv_data_.substr(num_bytes); 48 recv_data_ = recv_data_.substr(num_bytes);
80 } 49 }
81 50
82 } // namespace net 51 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698