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
|
/* $Id: http_connector_hit.c,v 6.19 2010/02/03 19:04:42 kazimird Exp $
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* Author: Denis Vakatov
*
* File Description:
* Hit an arbitrary URL using HTTP-based CONNECTOR
*
*/
#include <connect/ncbi_http_connector.h>
#include "../ncbi_ansi_ext.h"
#include "../ncbi_priv.h" /* CORE logging facilities */
/* This header must go last */
#include "test_assert.h"
/* Holder for the cmd.-line arg values describing the URL to hit
* (used by the pseudo-registry getter "s_REG_Get")
*/
static struct {
const char* host;
const char* port;
const char* path;
const char* args;
} s_Args;
/* Getter for the pseudo-registry
*/
#if defined(__cplusplus)
extern "C" {
static void s_REG_Get(void*user_data,
const char* section, const char* name,
char* value, size_t value_size);
}
#endif /* __cplusplus */
static void s_REG_Get
(void* user_data,
const char* section,
const char* name,
char* value,
size_t value_size)
{
if (strcmp(section, DEF_CONN_REG_SECTION) != 0) {
assert(0);
return;
}
#define X_GET_VALUE(x_name, x_value) \
if (strcmp(name, x_name) == 0) { \
strncpy0(value, x_value, value_size - 1); \
return; \
}
X_GET_VALUE(REG_CONN_HOST, s_Args.host);
X_GET_VALUE(REG_CONN_PORT, s_Args.port);
X_GET_VALUE(REG_CONN_PATH, s_Args.path);
X_GET_VALUE(REG_CONN_ARGS, s_Args.args);
X_GET_VALUE(REG_CONN_DEBUG_PRINTOUT, "yes");
}
/*****************************************************************************
* MAIN
*/
int main(int argc, const char* argv[])
{
const char* inp_file = (argc > 5) ? argv[5] : "";
const char* user_header = (argc > 6) ? argv[6] : "";
CONNECTOR connector;
CONN conn;
EIO_Status status;
char buffer[100];
size_t n_read, n_written;
/* Prepare to connect: parse and check cmd.-line args, etc. */
s_Args.host = (argc > 1) ? argv[1] : "";
s_Args.port = (argc > 2) ? argv[2] : "";
s_Args.path = (argc > 3) ? argv[3] : "";
s_Args.args = (argc > 4) ? argv[4] : "";
fprintf(stderr, "Running '%s':\n"
" URL host: '%s'\n"
" URL port: '%s'\n"
" URL path: '%s'\n"
" URL args: '%s'\n"
" Input data file: '%s'\n"
" User header: '%s'\n"
"Reply(if any) from the hit URL goes to the standard output.\n\n",
argv[0],
s_Args.host, s_Args.port, s_Args.path, s_Args.args,
inp_file, user_header);
/* Log stream */
CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level |
fLOG_OmitNoteLevel | fLOG_DateTime);
CORE_SetLOGFILE(stderr, 0/*false*/);
/* Tune to the test URL using hard-coded pseudo-registry */
CORE_SetREG( REG_Create(0, s_REG_Get, 0, 0, 0) );
/* Usage */
if (argc < 4) {
fprintf(stderr,
"Usage: %s host port path [args] [inp_file] [user_header]\n"
"Example: %s www.ncbi.nlm.nih.gov 80 "
"/Service/bounce.cgi 'arg1+arg2+arg3'\n",
argv[0], argv[0]);
fprintf(stderr, "Too few arguments.\n");
return 1;
}
/* Connect */
connector = HTTP_CreateConnector(0, user_header, 0);
assert(connector);
verify(CONN_Create(connector, &conn) == eIO_Success);
/* If the input file is specified,
* then send its content (as the HTTP request body) to URL
*/
if (*inp_file) {
FILE* inp_fp;
if (strcmp(inp_file, "-") != 0) {
static const char kDevNull[] =
#ifdef NCBI_OS_MSWIN
"NUL"
#else
"/dev/null"
#endif /*NCBI_OS_MSWIN*/
;
if (strcmp(inp_file, "+") == 0)
inp_file = kDevNull;
if (!(inp_fp = fopen(inp_file, "rb"))) {
fprintf(stderr, "Cannot open file '%s' for reading", inp_file);
assert(0);
}
} else
inp_fp = stdin;
for (;;) {
n_read = fread(buffer, 1, sizeof(buffer), inp_fp);
if (n_read <= 0) {
assert(feof(inp_fp));
break; /* EOF */
}
status = CONN_Write(conn, buffer, n_read,
&n_written, eIO_WritePersist);
if (status != eIO_Success) {
fprintf(stderr, "Error writing to URL (%s)",
IO_StatusStr(status));
assert(0);
}
assert(n_written == n_read);
}
fclose(inp_fp);
}
/* Read reply from connection, write it to standard output */
for (;;) {
status = CONN_Read(conn,buffer,sizeof(buffer),&n_read,eIO_ReadPlain);
if (status != eIO_Success)
break;
if (connector)
puts("----- [BEGIN] HTTP Content -----");
fwrite(buffer, 1, n_read, stdout);
fflush(stdout);
connector = 0;
}
if (!connector) {
puts("\n----- [END] HTTP Content -----");
fclose(stdout);
}
if (status != eIO_Closed) {
fprintf(stderr, "Error reading from URL (%s)", IO_StatusStr(status));
assert(0);
}
/* Success: close the connection, cleanup, and exit */
CONN_Close(conn);
CORE_SetREG(0);
CORE_LOG(eLOG_Note, "TEST completed successfully");
CORE_SetLOG(0);
return 0;
}
|