56 virtual void Init(
void);
57 virtual int Run(
void);
74 : m_PrintHeaders(
true),
86 if (args[
"timeout"]) {
89 if (args[
"retries"]) {
90 ret.
SetRetries((
unsigned short)args[
"retries"].AsInteger());
95 if (args[
"proxy-host"]) {
96 string phost = args[
"proxy-host"].AsString();
98 unsigned short pport = (
unsigned short)args[
"proxy-port"].AsInteger();
99 if (args[
"proxy-user"]) {
100 string puser = args[
"proxy-user"].AsString();
102 if (args[
"proxy-password"]) {
103 ppass = args[
"proxy-password"].AsString();
111 if ( args[
"deadline"] ) {
114 if ( args[
"retry-processing"] ) {
133 if (!cert.empty() && !pkey.empty()) {
134 m_Credentials = make_shared<CTlsCertCredentials>(cert, pkey);
145 arg_desc->SetUsageContext(
GetArguments().GetProgramBasename(),
146 "HTTP session sample application");
148 arg_desc->AddFlag(
"http-11",
"Use HTTP/1.1 protocol",
true);
149 arg_desc->AddFlag(
"http-2",
"Use HTTP/2 protocol",
true);
150 arg_desc->AddFlag(
"print-headers",
"Print HTTP response headers",
true);
151 arg_desc->AddFlag(
"print-cookies",
"Print HTTP cookies",
true);
152 arg_desc->AddFlag(
"print-body",
"Print HTTP response body",
true);
155 arg_desc->AddOptionalKey(
"head",
"url",
"URL to load using HEAD request",
158 arg_desc->AddOptionalKey(
"get",
"url",
"URL to load using GET request",
161 arg_desc->AddOptionalKey(
"post",
"url",
"URL to POST data to (the data is read from STDIN)",
165 arg_desc->AddOptionalKey(
"method",
"method",
"HTTP request method: HEAD/GET/POST",
168 allow_methods.
Allow(
"HEAD");
169 allow_methods.
Allow(
"GET");
170 allow_methods.
Allow(
"POST");
171 arg_desc->SetConstraint(
"method", allow_methods);
176 arg_desc->AddOptionalKey(
"url",
"url",
"URL to access using the specified method.",
180 arg_desc->AddOptionalKey(
"service-name",
"name",
"Named service to access using the specified method.",
185 arg_desc->AddOptionalKey(
"service-path",
"path",
"Path to be appended to the service name.",
189 arg_desc->AddOptionalKey(
"service-args",
"name",
"Named service arguments.",
194 arg_desc->AddOptionalKey(
"timeout",
"double",
"Timeout in seconds",
196 arg_desc->AddOptionalKey(
"retries",
"int",
"Number of retries",
198 arg_desc->AddOptionalKey(
"deadline",
"double",
"Deadline for request total processing, in seconds",
200 arg_desc->AddOptionalKey(
"retry-processing",
"bool",
"Whether to wait for actual response",
245 if ( args[
"http-11"] ) {
248 }
else if (!args[
"http-2"]) {
252 if (args[
"cert-file"]) {
253 LoadCredentials(args[
"cert-file"].AsInputFile(), args[
"pkey-file"].AsInputFile());
257 bool skip_defaults =
false;
258 if (args[
"method"]) {
260 if (args[
"method"].AsString() ==
"GET")
262 else if (args[
"method"].AsString() ==
"POST")
266 url.
SetUrl(args[
"url"].AsString());
268 else if (args[
"service-name"]) {
269 url.
SetService(args[
"service-name"].AsString());
270 if (args[
"service-path"]) {
271 url.
SetPath(args[
"service-path"].AsString());
273 if (args[
"service-args"]) {
278 cout <<
"Missing 'url' or 'service-name' argument for " << args[
"method"] <<
" request." << endl;
289 if ( args[
"head"] ) {
290 skip_defaults =
true;
291 const auto& urls = args[
"head"].GetStringList();
292 for (
const auto& url : urls) {
293 cout <<
"HEAD " << url << endl;
296 cout <<
"-------------------------------------" << endl << endl;
300 skip_defaults =
true;
301 const auto& urls = args[
"get"].GetStringList();
302 for (
const auto& url : urls) {
303 cout <<
"GET " << url << endl;
305 cout <<
"-------------------------------------" << endl;
307 cout <<
"-------------------------------------" << endl << endl;
310 if ( args[
"post"] ) {
311 skip_defaults =
true;
312 string url = args[
"post"].AsString();
313 cout <<
"POST " << url << endl;
317 cout <<
"-------------------------------------" << endl << endl;
320 if ( skip_defaults ) {
324 const string sample_url =
"https://dev.ncbi.nlm.nih.gov/Service/sample/cgi_sample.cgi";
325 const string bad_url =
"https://dev.ncbi.nlm.nih.gov/Service/sample/404";
326 CUrl url(sample_url);
330 cout <<
"HEAD " << sample_url << endl;
335 cout <<
"-------------------------------------" << endl << endl;
340 cout <<
"GET (no args) " << sample_url << endl;
343 cout <<
"-------------------------------------" << endl << endl;
348 cout <<
"GET (shortcut) " << sample_url << endl;
351 cout <<
"-------------------------------------" << endl << endl;
355 CUrl url_with_args(sample_url);
357 cout <<
"GET (with args) " << sample_url << endl;
361 cout <<
"-------------------------------------" << endl << endl;
366 cout <<
"POST (form data) " << sample_url << endl;
369 data.AddEntry(
"message",
"POST data");
371 cout <<
"-------------------------------------" << endl << endl;
376 cout <<
"POST (provider) " << sample_url << endl;
381 cout <<
"-------------------------------------" << endl << endl;
386 cout <<
"POST (manual) " << sample_url << endl;
390 out <<
"message=POST manual data";
392 cout <<
"-------------------------------------" << endl << endl;
397 cout <<
"POST (shortcut) " << sample_url << endl;
400 cout <<
"-------------------------------------" << endl << endl;
405 cout <<
"PUT (provider) " << sample_url << endl;
410 cout <<
"-------------------------------------" << endl << endl;
415 cout <<
"GET (404) " << bad_url << endl;
418 cout <<
"-------------------------------------" << endl << endl;
423 cout <<
"GET service: test" << endl;
426 cout <<
"-------------------------------------" << endl << endl;
431 cout <<
"GET service (shortcut): test" << endl;
434 cout <<
"-------------------------------------" << endl << endl;
438 cout <<
m_Errors <<
" requests failed." << endl;
452 cout <<
"--- Headers ---" << endl;
453 for (
const auto& header : response.
Headers().
Get()) {
454 for (
const auto&
value : header.second) {
455 cout << header.first <<
": " <<
value << endl;
461 cout <<
"--- Cookies ---" << endl;
462 for (
const auto& cookie : session->
Cookies()) {
468 cout <<
"--- Body ---" << endl;
CHttpSession and CHttpRequest parameters.
Per-request proxy settings.
virtual int Run(void)
Run the application.
shared_ptr< CTlsCertCredentials > m_Credentials
bool PrintResponse(const CHttp2Session *session, const CHttpResponse &response)
void LoadCredentials(CNcbiIstream &cert_str, CNcbiIstream &pkey_str)
CHttpParam SetupParam(void)
virtual void Init(void)
Initialize the application.
virtual string GetContentType(void) const
Get content type.
virtual void WriteData(CNcbiOstream &out) const
Write user data to the stream.
virtual string GetFileName(void) const
Get optional filename to be shown in Content-Disposition header.
CTimeout – Timeout interval.
The NCBI C++ standard methods for dealing with std::string.
std::ofstream out("events_result.xml")
main entry point for tests
static const char * str(char *buf, int n)
virtual void Init(void)
Initialize the application.
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
CArgAllow_Strings * Allow(const string &value)
Add allowed string values.
@ fAllowMultiple
Repeated key arguments are legal (use with AddKey)
@ fBinary
Open as binary file; for eInputFile, eOutputFile, eIOFile.
@ eRequires
One argument requires another.
@ eExcludes
One argument excludes another.
@ eInputFile
Name of file (must exist and be readable)
@ eBoolean
{'true', 't', 'false', 'f'}, case-insensitive
@ eDouble
Convertible into a floating point number (double)
@ eString
An arbitrary string.
@ eInteger
Convertible into an integer number (int or Int8)
const THeaders & Get() const
CHttpParam & SetRetries(THttpRetries retries)
CNcbiIstream & ContentStream(void) const
Get input stream.
int GetStatusCode(void) const
Get response status code.
void SetValue(CHeaderNameConverter name, CTempString value)
Remove all existing values with the name, set the new value.
const CHttpHeaders & Headers(void) const
Get incoming HTTP headers.
CNcbiIstream & ErrorStream(void) const
Get input stream containing error message (e.g.
CHttpParam & SetCredentials(shared_ptr< CTlsCertCredentials > credentials)
CNcbiOstream & ContentStream(void)
Get output stream to write user data.
CHttpParam & SetRetryProcessing(ESwitch on_off)
bool CanGetContentStream(void) const
Check if the requested content can be read from the content stream.
CHttpHeaders & Headers(void)
Get HTTP headers to be sent.
const string & GetStatusText(void) const
Get response status text.
CHttpResponse g_HttpPost(const CUrl &url, CTempString data, const CHttpParam ¶m=CHttpParam())
Shortcut for POST request.
const CHttpCookies & Cookies(void) const
Get all stored cookies.
CHttpParam & SetTimeout(const CTimeout &timeout)
void SetProtocol(EProtocol protocol)
CHttpRequest NewRequest(const CUrl &url, ERequestMethod method=eGet, const CHttpParam ¶m={})
Initialize request.
CHttpResponse Execute(void)
Send the request, initialize and return the response.
ERequestMethod
Supported request methods, proxy for EReqMethod.
void SetCredentials(shared_ptr< CTlsCertCredentials > cred)
Set TLS credentials.
CHttpResponse g_HttpGet(const CUrl &url, const CHttpParam ¶m)
Shortcut for GET request.
CHttpParam & SetDeadline(const CTimeout &deadline)
CHttpParam & SetProxy(const CHttpProxy &proxy)
CHttpFormData & FormData(void)
Get form data to be sent with POST request.
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
bool NcbiStreamCopy(CNcbiOstream &os, CNcbiIstream &is)
Copy the entire contents of stream "is" to stream "os".
size_t NcbiStreamToString(string *s, CNcbiIstream &is, size_t pos=0)
Input the entire contents of an istream into a string (NULL causes drain).
const CUrlArgs & GetArgs(void) const
Get const list of arguments.
void SetPath(const string &value)
void SetService(const string &value)
void SetQueryString(const string &query, NStr::EUrlEncode encode)
Parse query string, call AddArgument() to store each value.
void SetUrl(const string &url, const IUrlEncoder *encoder=0)
Parse the URL.
void SetValue(const string &name, const string &value)
Set new value for the first argument with the given name or add a new argument.
string ComposeUrl(CUrlArgs::EAmpEncoding amp_enc, const IUrlEncoder *encoder=0) const
Compose the URL.
@ eAmp_Char
Use & to separate arguments.
int NcbiSys_main(int argc, ncbi::TXChar *argv[])
static string LoadFile(CNcbiIstream &str)
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
std::istream & in(std::istream &in_, double &x_)