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

Side by Side Diff: net/test/embedded_test_server/request_handler_util.cc

Issue 1376593007: SSL in EmbeddedTestServer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing RunLoop and check. Created 5 years, 2 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
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "net/test/embedded_test_server/request_handler_util.h"
6
7 #include <stdlib.h>
8 #include <ctime>
9 #include <sstream>
10
11 #include "base/base64.h"
12 #include "base/files/file_util.h"
13 #include "base/format_macros.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "net/base/escape.h"
18 #include "net/base/url_util.h"
19 #include "net/http/http_byte_range.h"
20 #include "net/http/http_util.h"
21 #include "net/test/embedded_test_server/http_request.h"
22 #include "url/gurl.h"
23
24 namespace net {
25 namespace test_server {
26 namespace {
27
28 const UnescapeRule::Type kUnescapeAll =
29 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS |
30 UnescapeRule::SPOOFING_AND_CONTROL_CHARS |
31 UnescapeRule::REPLACE_PLUS_WITH_SPACE;
32
33 std::string GetContentType(const base::FilePath& path) {
34 if (path.MatchesExtension(FILE_PATH_LITERAL(".crx")))
35 return "application/x-chrome-extension";
36 if (path.MatchesExtension(FILE_PATH_LITERAL(".exe")))
37 return "application/octet-stream";
38 if (path.MatchesExtension(FILE_PATH_LITERAL(".gif")))
39 return "image/gif";
40 if (path.MatchesExtension(FILE_PATH_LITERAL(".jpeg")) ||
41 path.MatchesExtension(FILE_PATH_LITERAL(".jpg"))) {
42 return "image/jpeg";
43 }
44 if (path.MatchesExtension(FILE_PATH_LITERAL(".js")))
45 return "application/javascript";
46 if (path.MatchesExtension(FILE_PATH_LITERAL(".json")))
47 return "application/json";
48 if (path.MatchesExtension(FILE_PATH_LITERAL(".pdf")))
49 return "application/pdf";
50 if (path.MatchesExtension(FILE_PATH_LITERAL(".txt")))
51 return "text/plain";
52 if (path.MatchesExtension(FILE_PATH_LITERAL(".wav")))
53 return "audio/wav";
54 if (path.MatchesExtension(FILE_PATH_LITERAL(".xml")))
55 return "text/xml";
56 if (path.MatchesExtension(FILE_PATH_LITERAL(".html")) ||
57 path.MatchesExtension(FILE_PATH_LITERAL(".htm"))) {
58 return "text/html";
59 }
60 return "";
61 }
62
63 } // namespace
64
65 CustomHttpResponse::CustomHttpResponse(const std::string& headers,
66 const std::string& contents)
67 : headers_(headers), contents_(contents) {}
68
69 CustomHttpResponse::~CustomHttpResponse() {}
70
71 void CustomHttpResponse::SendResponse(const SendBytesCallback& send,
72 const SendCompleteCallback& done) {
73 std::string response;
74 if (!headers_.empty())
75 response = headers_ + "\r\n" + contents_;
76 else
77 response = contents_;
78 send.Run(response, done);
79 }
80
81 bool ShouldHandle(const HttpRequest& request, const std::string& path_prefix) {
82 GURL url = request.GetURL();
83 return url.path() == path_prefix ||
84 base::StartsWith(url.path(), path_prefix + "/",
85 base::CompareCase::SENSITIVE);
86 }
87
88 scoped_ptr<HttpResponse> HandlePrefixedRequest(
89 const std::string& prefix,
90 const EmbeddedTestServer::HandleRequestCallback& handler,
91 const HttpRequest& request) {
92 if (ShouldHandle(request, prefix))
93 return handler.Run(request);
94 return nullptr;
95 }
96
97 RequestQuery ParseQuery(const GURL& url) {
98 RequestQuery queries;
99 for (QueryIterator it(url); !it.IsAtEnd(); it.Advance()) {
100 queries[net::UnescapeURLComponent(it.GetKey(), kUnescapeAll)].push_back(
101 it.GetUnescapedValue());
102 }
103 return queries;
104 }
105
106 void GetFilePathWithReplacements(const std::string& original_file_path,
107 const base::StringPairs& text_to_replace,
108 std::string* replacement_path) {
109 std::string new_file_path = original_file_path;
110 for (const auto& replacement : text_to_replace) {
111 const std::string& old_text = replacement.first;
112 const std::string& new_text = replacement.second;
113 std::string base64_old;
114 std::string base64_new;
115 base::Base64Encode(old_text, &base64_old);
116 base::Base64Encode(new_text, &base64_new);
117 if (new_file_path == original_file_path)
118 new_file_path += "?";
119 else
120 new_file_path += "&";
121 new_file_path += "replace_text=";
122 new_file_path += base64_old;
123 new_file_path += ":";
124 new_file_path += base64_new;
125 }
126
127 *replacement_path = new_file_path;
128 }
129
130 // Handles |request| by serving a file from under |server_root|.
131 scoped_ptr<HttpResponse> HandleFileRequest(const base::FilePath& server_root,
132 const HttpRequest& request) {
133 // This is a test-only server. Ignore I/O thread restrictions.
134 // TODO(svaldez): Figure out why thread is I/O restricted in the first place.
135 base::ThreadRestrictions::ScopedAllowIO allow_io;
136
137 // A proxy request will have an absolute path. Simulate the proxy by stripping
138 // the scheme, host, and port.
139 GURL request_url = request.GetURL();
140 std::string relative_path(request_url.path());
141
142 std::string post_prefix("/post/");
143 if (base::StartsWith(relative_path, post_prefix,
144 base::CompareCase::SENSITIVE)) {
145 if (request.method != METHOD_POST)
146 return nullptr;
147 relative_path = relative_path.substr(post_prefix.size() - 1);
148 }
149
150 RequestQuery query = ParseQuery(request_url);
151
152 scoped_ptr<BasicHttpResponse> failed_response(new BasicHttpResponse);
153 failed_response->set_code(HTTP_NOT_FOUND);
154
155 if (query.find("expected_body") != query.end()) {
156 if (request.content.find(query["expected_body"].front()) ==
157 std::string::npos) {
158 return failed_response.Pass();
159 }
160 }
161
162 if (query.find("expected_headers") != query.end()) {
163 for (const auto& header : query["expected_headers"]) {
164 if (header.find(":") == std::string::npos)
165 return failed_response.Pass();
166 std::string key = header.substr(0, header.find(":"));
167 std::string value = header.substr(header.find(":") + 1);
168 if (request.headers.find(key) == request.headers.end() ||
169 request.headers.at(key) != value) {
170 return failed_response.Pass();
171 }
172 }
173 }
174
175 // Trim the first byte ('/').
176 DCHECK(base::StartsWith(relative_path, "/", base::CompareCase::SENSITIVE));
177 std::string request_path = relative_path.substr(1);
178 base::FilePath file_path(server_root.AppendASCII(request_path));
179 std::string file_contents;
180 if (!base::ReadFileToString(file_path, &file_contents)) {
181 file_path = file_path.AppendASCII("index.html");
182 if (!base::ReadFileToString(file_path, &file_contents))
183 return nullptr;
184 }
185
186 if (request.method == METHOD_HEAD)
187 file_contents = "";
188
189 if (query.find("replace_text") != query.end()) {
190 for (const auto& replacement : query["replace_text"]) {
191 if (replacement.find(":") == std::string::npos)
192 return failed_response.Pass();
193 std::string find;
194 std::string with;
195 base::Base64Decode(replacement.substr(0, replacement.find(":")), &find);
196 base::Base64Decode(replacement.substr(replacement.find(":") + 1), &with);
197 base::ReplaceSubstringsAfterOffset(&file_contents, 0, find, with);
198 }
199 }
200
201 base::FilePath headers_path(
202 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers")));
203
204 if (base::PathExists(headers_path)) {
205 std::string headers_contents;
206
207 if (!base::ReadFileToString(headers_path, &headers_contents))
208 return nullptr;
209
210 return make_scoped_ptr(
211 new CustomHttpResponse(headers_contents, file_contents));
212 }
213
214 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
215 http_response->set_code(HTTP_OK);
216
217 if (request.headers.find("Range") != request.headers.end()) {
218 std::vector<HttpByteRange> ranges;
219
220 if (HttpUtil::ParseRangeHeader(request.headers.at("Range"), &ranges) &&
221 ranges.size() == 1) {
222 ranges[0].ComputeBounds(file_contents.size());
223 size_t start = ranges[0].first_byte_position();
224 size_t end = ranges[0].last_byte_position();
225
226 http_response->set_code(HTTP_PARTIAL_CONTENT);
227 http_response->AddCustomHeader(
228 "Content-Range",
229 base::StringPrintf("bytes %" PRIuS "-%" PRIuS "/%" PRIuS, start, end,
230 file_contents.size()));
231
232 file_contents = file_contents.substr(start, end - start + 1);
233 }
234 }
235
236 http_response->set_content_type(GetContentType(file_path));
237 http_response->AddCustomHeader("Accept-Ranges", "bytes");
238 http_response->AddCustomHeader("ETag", "'" + file_path.MaybeAsASCII() + "'");
239 http_response->set_content(file_contents);
240 return http_response.Pass();
241 }
242
243 } // namespace test_server
244 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698