proxygen
CurlClientMain.cpp File Reference

Go to the source code of this file.

Functions

 DEFINE_string (http_method,"GET","HTTP method to use. GET or POST are supported")
 
 DEFINE_string (url,"https://github.com/facebook/proxygen","URL to perform the HTTP method against")
 
 DEFINE_string (input_filename,"","Filename to read from for POST requests")
 
 DEFINE_int32 (http_client_connect_timeout, 1000,"connect timeout in milliseconds")
 
 DEFINE_string (ca_path,"/etc/ssl/certs/ca-certificates.crt","Path to trusted CA file")
 
 DEFINE_string (cert_path,"","Path to client certificate file")
 
 DEFINE_string (key_path,"","Path to client private key file")
 
 DEFINE_string (next_protos,"h2,h2-14,spdy/3.1,spdy/3,http/1.1","Next protocol string for NPN/ALPN")
 
 DEFINE_string (plaintext_proto,"","plaintext protocol")
 
 DEFINE_int32 (recv_window, 65536,"Flow control receive window for h2/spdy")
 
 DEFINE_bool (h2c, true,"Attempt HTTP/1.1 -> HTTP/2 upgrade")
 
 DEFINE_string (headers,"","List of N=V headers separated by ,")
 
 DEFINE_string (proxy,"","HTTP proxy URL")
 
int main (int argc, char *argv[])
 

Function Documentation

DEFINE_bool ( h2c  ,
true  ,
"Attempt HTTP/1.1 -> HTTP/2 upgrade"   
)
DEFINE_int32 ( http_client_connect_timeout  ,
1000  ,
"connect timeout in milliseconds"   
)
DEFINE_int32 ( recv_window  ,
65536  ,
"Flow control receive window for h2/spdy"   
)
DEFINE_string ( http_method  ,
"GET"  ,
"HTTP method to use. GET or POST are supported"   
)
DEFINE_string ( url  ,
"https://github.com/facebook/proxygen"  ,
"URL to perform the HTTP method against"   
)
DEFINE_string ( input_filename  ,
""  ,
"Filename to read from for POST requests  
)
DEFINE_string ( ca_path  ,
"/etc/ssl/certs/ca-certificates.crt"  ,
"Path to trusted CA file"   
)
DEFINE_string ( cert_path  ,
""  ,
"Path to client certificate file"   
)
DEFINE_string ( key_path  ,
""  ,
"Path to client private key file"   
)
DEFINE_string ( next_protos  ,
h2,
h2-  14,
spdy/3.  1,
spdy/  3,
http/1.1"  ,
"Next protocol string for NPN/ALPN"   
)
DEFINE_string ( plaintext_proto  ,
""  ,
"plaintext protocol"   
)
DEFINE_string ( headers  ,
""  ,
"List of  N = V headers separated by,
 
)
DEFINE_string ( proxy  ,
""  ,
"HTTP proxy URL  
)
int main ( int  argc,
char *  argv[] 
)

Definition at line 50 of file CurlClientMain.cpp.

References addr, folly::AsyncSocket::anyAddress(), proxygen::ERROR, f, proxygen::URL::getHost(), proxygen::URL::getPort(), folly::INFO, proxygen::URL::isSecure(), folly::EventBase::loop(), CurlService::CurlClient::parseHeaders(), CurlService::CurlClient::setFlowControlSettings(), proxygen::HTTPConnector::setPlaintextProtocol(), and proxygen::stringToMethod().

50  {
51  gflags::ParseCommandLineFlags(&argc, &argv, true);
52  google::InitGoogleLogging(argv[0]);
53  google::InstallFailureSignalHandler();
54 
55  EventBase evb;
56  URL url(FLAGS_url);
57  URL proxy(FLAGS_proxy);
58 
59  if (FLAGS_http_method != "GET" && FLAGS_http_method != "POST") {
60  LOG(ERROR) << "http_method must be either GET or POST";
61  return EXIT_FAILURE;
62  }
63 
64  HTTPMethod httpMethod = *stringToMethod(FLAGS_http_method);
65  if (httpMethod == HTTPMethod::POST) {
66  try {
67  File f(FLAGS_input_filename);
68  (void)f;
69  } catch (const std::system_error& se) {
70  LOG(ERROR) << "Couldn't open file for POST method";
71  LOG(ERROR) << se.what();
72  return EXIT_FAILURE;
73  }
74  }
75 
76  HTTPHeaders headers = CurlClient::parseHeaders(FLAGS_headers);
77 
78  CurlClient curlClient(&evb,
79  httpMethod,
80  url,
81  FLAGS_proxy.empty() ? nullptr : &proxy,
82  headers,
83  FLAGS_input_filename,
84  FLAGS_h2c);
85  curlClient.setFlowControlSettings(FLAGS_recv_window);
86 
88  if (!FLAGS_proxy.empty()) {
89  addr = SocketAddress(proxy.getHost(), proxy.getPort(), true);
90  } else {
91  addr = SocketAddress(url.getHost(), url.getPort(), true);
92  }
93  LOG(INFO) << "Trying to connect to " << addr;
94 
95  // Note: HHWheelTimer is a large object and should be created at most
96  // once per thread
97  HHWheelTimer::UniquePtr timer{HHWheelTimer::newTimer(
98  &evb,
99  std::chrono::milliseconds(HHWheelTimer::DEFAULT_TICK_INTERVAL),
100  AsyncTimeout::InternalEnum::NORMAL,
101  std::chrono::milliseconds(5000))};
102  HTTPConnector connector(&curlClient, timer.get());
103  if (!FLAGS_plaintext_proto.empty()) {
104  connector.setPlaintextProtocol(FLAGS_plaintext_proto);
105  }
106  static const AsyncSocket::OptionMap opts{{{SOL_SOCKET, SO_REUSEADDR}, 1}};
107 
108  if (url.isSecure()) {
109  curlClient.initializeSsl(
110  FLAGS_ca_path, FLAGS_next_protos, FLAGS_cert_path, FLAGS_key_path);
111  connector.connectSSL(
112  &evb,
113  addr,
114  curlClient.getSSLContext(),
115  nullptr,
116  std::chrono::milliseconds(FLAGS_http_client_connect_timeout),
117  opts,
119  curlClient.getServerName());
120  } else {
121  connector.connect(&evb, addr,
122  std::chrono::milliseconds(FLAGS_http_client_connect_timeout), opts);
123  }
124 
125  evb.loop();
126 
127  return EXIT_SUCCESS;
128 }
folly::Optional< HTTPMethod > stringToMethod(folly::StringPiece method)
Definition: HTTPMethod.cpp:37
auto f
void setFlowControlSettings(int32_t recvWindow)
Definition: CurlClient.cpp:108
char ** argv
std::map< OptionKey, int > OptionMap
Definition: AsyncSocket.h:376
static const folly::SocketAddress & anyAddress()
void setPlaintextProtocol(const std::string &plaintextProto)
std::unique_ptr< HHWheelTimer, Destructor > UniquePtr
Definition: HHWheelTimer.h:57
ErrorCode parseHeaders(Cursor &cursor, const FrameHeader &header, folly::Optional< PriorityUpdate > &outPriority, std::unique_ptr< IOBuf > &outBuf) noexcept
ThreadPoolListHook * addr