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

Side by Side Diff: components/html_viewer/blink_url_request_type_converters.cc

Issue 1677293002: Bye bye Mandoline (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: moar Created 4 years, 10 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 2014 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 "components/html_viewer/blink_url_request_type_converters.h"
6
7 #include <stdint.h>
8 #include <utility>
9
10 #include "base/strings/string_util.h"
11 #include "mojo/public/cpp/system/data_pipe.h"
12 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
13 #include "third_party/WebKit/public/platform/WebURLRequest.h"
14
15 namespace mojo {
16 namespace {
17
18 // Ripped from web_url_loader_impl.cc.
19 class HeaderFlattener : public blink::WebHTTPHeaderVisitor {
20 public:
21 HeaderFlattener() : has_accept_header_(false) {}
22
23 void visitHeader(const blink::WebString& name,
24 const blink::WebString& value) override {
25 // Headers are latin1.
26 const std::string& name_latin1 = name.latin1();
27 const std::string& value_latin1 = value.latin1();
28
29 if (base::LowerCaseEqualsASCII(name_latin1, "accept"))
30 has_accept_header_ = true;
31
32 HttpHeaderPtr header = HttpHeader::New();
33 header->name = name_latin1;
34 header->value = value_latin1;
35 buffer_.push_back(std::move(header));
36 }
37
38 Array<HttpHeaderPtr> GetBuffer() {
39 // In some cases, WebKit doesn't add an Accept header, but not having the
40 // header confuses some web servers. See bug 808613.
41 if (!has_accept_header_) {
42 HttpHeaderPtr header = HttpHeader::New();
43 header->name = "Accept";
44 header->value = "*/*";
45 buffer_.push_back(std::move(header));
46 has_accept_header_ = true;
47 }
48 return std::move(buffer_);
49 }
50
51 private:
52 Array<HttpHeaderPtr> buffer_;
53 bool has_accept_header_;
54 };
55
56 void AddRequestBody(URLRequest* url_request,
57 const blink::WebURLRequest& request) {
58 if (request.httpBody().isNull())
59 return;
60
61 uint32_t i = 0;
62 blink::WebHTTPBody::Element element;
63 while (request.httpBody().elementAt(i++, element)) {
64 switch (element.type) {
65 case blink::WebHTTPBody::Element::TypeData:
66 if (!element.data.isEmpty()) {
67 // WebKit sometimes gives up empty data to append. These aren't
68 // necessary so we just optimize those out here.
69 uint32_t num_bytes = static_cast<uint32_t>(element.data.size());
70 MojoCreateDataPipeOptions options;
71 options.struct_size = sizeof(MojoCreateDataPipeOptions);
72 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
73 options.element_num_bytes = 1;
74 options.capacity_num_bytes = num_bytes;
75 DataPipe data_pipe(options);
76 url_request->body.push_back(std::move(data_pipe.consumer_handle));
77 WriteDataRaw(data_pipe.producer_handle.get(),
78 element.data.data(),
79 &num_bytes,
80 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
81 }
82 break;
83 case blink::WebHTTPBody::Element::TypeFile:
84 case blink::WebHTTPBody::Element::TypeFileSystemURL:
85 case blink::WebHTTPBody::Element::TypeBlob:
86 // TODO(mpcomplete): handle these.
87 NOTIMPLEMENTED();
88 break;
89 default:
90 NOTREACHED();
91 }
92 }
93 }
94
95 } // namespace
96
97 URLRequestPtr TypeConverter<URLRequestPtr, blink::WebURLRequest>::Convert(
98 const blink::WebURLRequest& request) {
99 URLRequestPtr url_request(URLRequest::New());
100 url_request->url = request.url().string().utf8();
101 url_request->method = request.httpMethod().utf8();
102
103 HeaderFlattener flattener;
104 request.visitHTTPHeaderFields(&flattener);
105 url_request->headers = flattener.GetBuffer();
106
107 AddRequestBody(url_request.get(), request);
108
109 return url_request;
110 }
111
112 } // namespace mojo
113
OLDNEW
« no previous file with comments | « components/html_viewer/blink_url_request_type_converters.h ('k') | components/html_viewer/content_handler_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698