forked from YahooArchive/cnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnet_util.cc
292 lines (265 loc) · 10.7 KB
/
cnet_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2014, Yahoo! Inc.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Some portions of this file:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdio.h>
#include <stdlib.h>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/time/time.h"
#include "url/gurl.h"
#include "yahoo/cnet/cnet.h"
#if defined(OS_MACOSX)
#include "base/mac/scoped_nsautorelease_pool.h"
#endif
#if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
#include "base/i18n/icu_util.h"
#endif
namespace {
// LICENSE: copied from base/strings/string_split.cc
// (it isn't exposed via a header file)
bool SplitStringIntoKeyValue(const std::string& line,
char key_value_delimiter,
std::string* key,
std::string* value) {
key->clear();
value->clear();
// Find the delimiter.
size_t end_key_pos = line.find_first_of(key_value_delimiter);
if (end_key_pos == std::string::npos) {
DVLOG(1) << "cannot find delimiter in: " << line;
return false; // no delimiter
}
key->assign(line, 0, end_key_pos);
// Find the value string.
std::string remains(line, end_key_pos, line.size() - end_key_pos);
size_t begin_value_pos = remains.find_first_not_of(key_value_delimiter);
if (begin_value_pos == std::string::npos) {
DVLOG(1) << "cannot parse value from line: " << line;
return false; // no value
}
value->assign(remains, begin_value_pos, remains.size() - begin_value_pos);
return true;
}
void GetCompletion(CnetFetcher fetcher, CnetResponse response, void* param) {
// On network thread.
const char* initial_url = CnetResponseInitialUrl(response);
int http_response_code = CnetResponseHttpCode(response);
const char* response_body = CnetResponseBody(response);
int response_len = CnetResponseLength(response);
const CnetLoadTiming* timing = CnetResponseTiming(response);
char* content_type = CnetResponseFirstHeaderCopy(response, "Content-Type");
if (response_body != NULL) {
write(STDOUT_FILENO, response_body, response_len);
}
if (initial_url != NULL) {
LOG(INFO) << "Initial URL: " << initial_url;
}
LOG(INFO) << "HTTP result: " << http_response_code;
if (content_type != NULL) {
LOG(INFO) << "Content-Type: " << content_type;
}
LOG(INFO) << "size: " << response_len;
LOG(INFO) << "dns (ms): " << timing->dns_ms;
LOG(INFO) << "connect (ms): " << timing->connect_ms;
LOG(INFO) << "ssl (ms): " << timing->ssl_ms;
LOG(INFO) << "proxy (ms): " << timing->proxy_resolve_ms;
LOG(INFO) << "send (ms): " << timing->send_ms;
LOG(INFO) << "headers receive (ms): " << timing->headers_receive_ms;
LOG(INFO) << "data receive (ms): " << timing->data_receive_ms;
if (CnetResponseWasCached(response)) {
LOG(INFO) << "from cache";
}
if (CnetResponseWasFetchedViaProxy(response)) {
LOG(INFO) << "fetched via a proxy";
}
if (CnetResponseWasFetchedViaHttp(response)) {
LOG(INFO) << "fetched via HTTP";
}
if (CnetResponseWasFetchedViaSpdy(response)) {
LOG(INFO) << "fetched via SPDY";
}
if (CnetResponseWasFetchedViaQuic(response)) {
LOG(INFO) << "fetched via QUIC";
}
if (content_type != NULL) {
free(content_type);
}
CnetPool pool = CnetFetcherPool(fetcher);
CnetFetcherRelease(fetcher);
CnetPoolRelease(pool);
// Quit the main loop, but give some time for the pool threads to
// join with the UI thread.
base::MessageLoopForUI* ui_loop = (base::MessageLoopForUI*)param;
ui_loop->PostDelayedTask(FROM_HERE, base::MessageLoopForUI::QuitClosure(),
base::TimeDelta::FromMilliseconds(300));
}
void UploadProgress(CnetFetcher fetcher, void* param,
int64_t current, int64_t expected) {
// On network thread.
LOG(INFO) << "sent " << current << " of " << expected;
}
void GetProgress(CnetFetcher fetcher, void* param,
int64_t current, int64_t expected) {
// On network thread.
LOG(INFO) << "got " << current << " of " << expected;
}
} // namespace
int main(int argc, char* argv[]) {
#if defined(OS_MACOSX)
base::mac::ScopedNSAutoreleasePool auto_release_pool;
#endif
base::AtExitManager exit_manager;
base::StatisticsRecorder::Initialize();
#if !defined(USE_ICU_ALTERNATIVES_ON_ANDROID)
base::i18n::InitializeICU();
#endif
base::CommandLine::Init(argc, argv);
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
std::string cache_path(command_line.GetSwitchValueASCII("cache"));
std::string proxy_rules(command_line.GetSwitchValueASCII("proxy-rules"));
std::string cache_behavior(command_line.GetSwitchValueASCII("cache-behavior"));
bool trust_all_cert_authorities(command_line.HasSwitch(
"trust-all-cert-authorities"));
std::string oauth_app_key(command_line.GetSwitchValueASCII("oauth-app-key"));
std::string oauth_app_secret(command_line.GetSwitchValueASCII(
"oauth-app-secret"));
std::string oauth_token(command_line.GetSwitchValueASCII("oauth-token"));
std::string oauth_token_secret(command_line.GetSwitchValueASCII(
"oauth-token-secret"));
std::string method(command_line.GetSwitchValueASCII("method"));
if (method.empty()) {
method = "GET";
}
std::string body(command_line.GetSwitchValueASCII("body"));
std::string body_type(command_line.GetSwitchValueASCII("body-type"));
base::FilePath upload_path(command_line.GetSwitchValueASCII("upload"));
std::string upload_content_type(command_line.GetSwitchValueASCII(
"upload-type"));
std::string upload_key(command_line.GetSwitchValueASCII("upload-key"));
std::string output_path(command_line.GetSwitchValueASCII("output-path"));
std::string min_speed(command_line.GetSwitchValueASCII("min-speed"));
std::string quic_host(command_line.GetSwitchValueASCII("quic-host"));
std::string quic_port_str(command_line.GetSwitchValueASCII("quic-port"));
base::MessageLoopForUI ui_loop;
CnetInitialize(1);
CnetPoolConfig pool_config;
CnetPoolDefaultConfigPrepare(&pool_config);
pool_config.user_agent = "cnet-util";
pool_config.enable_spdy = 1;
pool_config.enable_quic = 1;
pool_config.cache_path = (cache_path.empty()) ? NULL:cache_path.c_str();
pool_config.cache_max_bytes = 40*1024;
pool_config.trust_all_cert_authorities = trust_all_cert_authorities;
pool_config.log_level = 1;
CnetPool pool = CnetPoolCreate(static_cast<CnetMessageLoopForUi>(&ui_loop),
pool_config);
CnetPoolSetProxy(pool, proxy_rules.c_str());
if (!quic_host.empty() && !quic_port_str.empty()) {
unsigned quic_port;
if (base::StringToUint(quic_port_str, &quic_port)) {
if (quic_port > std::numeric_limits<uint16>::min() &&
quic_port <= std::numeric_limits<uint16>::max()) {
CnetPoolAddQuicHint(pool, quic_host.c_str(), 80, quic_port);
CnetPoolAddQuicHint(pool, quic_host.c_str(), 443, quic_port);
}
}
}
std::string params_encoding(command_line.GetSwitchValueASCII("encoding"));
base::StringPairs fetch_params;
base::StringPairs fetch_headers;
GURL url;
base::CommandLine::StringVector args = command_line.GetArgs();
for (base::CommandLine::StringVector::const_iterator arg_it = args.begin();
arg_it != args.end(); arg_it++) {
GURL url_try = GURL(*arg_it);
if (url_try.is_valid() && url_try.IsStandard()) {
url = url_try;
} else {
std::string key;
std::string value;
if (SplitStringIntoKeyValue(*arg_it, '=', &key, &value)) {
std::pair<std::string, std::string> pair(key, value);
fetch_params.push_back(pair);
} else if (SplitStringIntoKeyValue(*arg_it, ':', &key, &value)) {
std::pair<std::string, std::string> pair(key, value);
fetch_headers.push_back(pair);
}
}
}
CnetFetcher fetcher = CnetFetcherCreate(pool, url.spec().c_str(),
method.c_str(), &ui_loop, GetCompletion, GetProgress, UploadProgress);
if (fetcher != NULL) {
if (!min_speed.empty()) {
double bytes_sec = 0;
if (base::StringToDouble(min_speed, &bytes_sec)) {
CnetFetcherSetMinSpeed(fetcher, bytes_sec, 1.0);
}
}
if (!output_path.empty()) {
CnetFetcherSetOutputFile(fetcher, output_path.c_str());
}
if (!oauth_app_key.empty()) {
CnetFetcherSetOauthCredentials(fetcher,
oauth_app_key.c_str(), oauth_app_secret.c_str(),
oauth_token.c_str(), oauth_token_secret.c_str());
}
if (params_encoding == "url") {
CnetFetcherSetUrlParamsEncoding(fetcher, CNET_ENCODE_URL);
} else if (params_encoding == "body-url") {
CnetFetcherSetUrlParamsEncoding(fetcher, CNET_ENCODE_BODY_URL);
} else if (params_encoding == "body-multipart") {
CnetFetcherSetUrlParamsEncoding(fetcher, CNET_ENCODE_BODY_MULTIPART);
}
if (cache_behavior == "validate") {
CnetFetcherSetCacheBehavior(fetcher, CNET_CACHE_VALIDATE);
} else if (cache_behavior == "bypass") {
CnetFetcherSetCacheBehavior(fetcher, CNET_CACHE_BYPASS);
} else if (cache_behavior == "prefer") {
CnetFetcherSetCacheBehavior(fetcher, CNET_CACHE_PREFER);
} else if (cache_behavior == "only") {
CnetFetcherSetCacheBehavior(fetcher, CNET_CACHE_ONLY);
} else if (cache_behavior == "offline") {
CnetFetcherSetCacheBehavior(fetcher, CNET_CACHE_IF_OFFLINE);
} else if (cache_behavior == "disable") {
CnetFetcherSetCacheBehavior(fetcher, CNET_CACHE_DISABLE);
}
for (base::StringPairs::const_iterator it = fetch_headers.begin();
it != fetch_headers.end(); ++it) {
CnetFetcherSetHeader(fetcher, it->first.c_str(), it->second.c_str());
}
for (base::StringPairs::const_iterator it = fetch_params.begin();
it != fetch_params.end(); ++it) {
CnetFetcherSetUrlParam(fetcher, it->first.c_str(), it->second.c_str());
}
if (!body.empty()) {
CnetFetcherSetUploadBody(fetcher, body_type.c_str(), body.c_str());
} else if (!upload_path.empty()) {
if (params_encoding == "body-multipart") {
CnetFetcherSetUrlParamFile(fetcher, upload_key.c_str(),
upload_path.BaseName().AsUTF8Unsafe().c_str(),
upload_content_type.c_str(), upload_path.AsUTF8Unsafe().c_str(),
0, kuint64max);
} else {
CnetFetcherSetUploadFile(fetcher, upload_content_type.c_str(),
upload_path.AsUTF8Unsafe().c_str(), 0, kuint64max);
}
}
CnetFetcherStart(fetcher);
ui_loop.Run();
} else {
CnetPoolRelease(pool);
}
CnetCleanup();
return EXIT_SUCCESS;
}