summaryrefslogtreecommitdiff
path: root/connect/test/test_ncbi_http_get.c
blob: 1e2d2568c4b6c4d3614173cc055f84cef0702f72 (plain)
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
/* $Id: test_ncbi_http_get.c,v 6.19 2008/11/10 17:14: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:  Anton Lavrentiev
 *
 * File Description:
 *   Retrieve a Web document via HTTP
 *
 */

#include "../ncbi_ansi_ext.h"
#include "../ncbi_priv.h"
#include <connect/ncbi_gnutls.h>
#include <connect/ncbi_http_connector.h>
#include <connect/ncbi_util.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#ifdef NCBI_OS_UNIX
#  include <unistd.h>
#endif
/* This header must go last */
#include "test_assert.h"


int main(int argc, char* argv[])
{
    CONNECTOR     connector;
    SConnNetInfo* net_info;
    char          blk[512];
    EIO_Status    status;
    THCC_Flags    flags;
    CONN          conn;
    FILE*         fp;
    time_t        t;
    size_t        n;

    CORE_SetLOGFormatFlags(fLOG_None          | fLOG_Level   |
                           fLOG_OmitNoteLevel | fLOG_DateTime);
    CORE_SetLOGFILE(stderr, 0/*false*/);

    if (argc < 2  ||  !*argv[1])
        CORE_LOG(eLOG_Fatal, "URL has to be supplied on the command line");
    if (argc > 3)
        CORE_LOG(eLOG_Fatal, "Command cannot take more than 2 arguments");
    if (argc == 3) {
        fp = strcmp(argv[2], "-") == 0 ? stdin : fopen(argv[2], "rb");
        if (!fp) {
            CORE_LOGF_ERRNO(eLOG_Error, errno, ("Cannot open \"%s\"",
                                                argv[2] ? argv[2] : ""));
        }
    } else
        fp = 0;

    ConnNetInfo_GetValue(0, "RECONNECT", blk, 32, "");
    if (*blk  &&  (strcmp    (blk, "1")    == 0  ||
                   strcasecmp(blk, "ON")   == 0  ||
                   strcasecmp(blk, "YES")  == 0  ||
                   strcasecmp(blk, "TRUE") == 0)) {
        CORE_LOG(eLOG_Note, "Reconnect mode acknowledged");
        flags = fHCC_AutoReconnect;
    } else
        flags = 0;

    ConnNetInfo_GetValue(0, "USESSL", blk, 32, "");
    if (*blk  &&  (strcmp    (blk, "1")    == 0  ||
                   strcasecmp(blk, "ON")   == 0  ||
                   strcasecmp(blk, "YES")  == 0  ||
                   strcasecmp(blk, "TRUE") == 0)) {
#ifdef HAVE_LIBGNUTLS
        CORE_LOG(eLOG_Note,    "SSL request acknowledged");
        SOCK_SetupSSL(NcbiSetupGnuTls);
#else
        CORE_LOG(eLOG_Warning, "SSL requested but may not be supported");
#endif /*HAVE_LIBGNUTLS*/
    }

    CORE_LOG(eLOG_Note, "Creating network info structure");
    if (!(net_info = ConnNetInfo_Create(0)))
        CORE_LOG(eLOG_Fatal, "Cannot create network info structure");

    CORE_LOGF(eLOG_Note, ("Parsing URL \"%s\"", argv[1]));
    if (!ConnNetInfo_ParseURL(net_info, argv[1]))
        CORE_LOG(eLOG_Fatal, "URL parse failed");

    CORE_LOGF(eLOG_Note, ("Creating HTTP%s connector",
                          &"S"[net_info->scheme != eURL_Https]));
    if (!(connector = HTTP_CreateConnector(net_info, 0, flags)))
        CORE_LOG(eLOG_Fatal, "Cannot create HTTP connector");

    CORE_LOG(eLOG_Note, "Creating connection");
    if (CONN_Create(connector, &conn) != eIO_Success)
        CORE_LOG(eLOG_Fatal, "Cannot create connection");
    CONN_SetTimeout(conn, eIO_Open,      net_info->timeout);
    CONN_SetTimeout(conn, eIO_ReadWrite, net_info->timeout);
    while (fp  &&  !feof(fp)) {
        n = fread(blk, 1, sizeof(blk), fp);
        status = CONN_Write(conn, blk, n, &n, eIO_WritePersist);
        if (status != eIO_Success) {
            CORE_LOGF(eLOG_Fatal, ("Write error: %s", IO_StatusStr(status)));
        }
    }
    if (fp)
        fclose(fp);

    t = time(0);
    do {
        status = CONN_Wait(conn, eIO_Read, net_info->timeout);
        if (status != eIO_Success) {
            if (status != eIO_Timeout)
                break;
            if ((net_info->timeout  &&
                 (net_info->timeout->sec | net_info->timeout->usec))
                ||  (unsigned long)(time(0) - t) > DEF_CONN_TIMEOUT)
                CORE_LOG(eLOG_Fatal, "Timed out");
#ifdef NCBI_OS_UNIX
            usleep(500);
#endif /*NCBI_OS_UNIX*/
            continue;
        }

        status = CONN_ReadLine(conn, blk, sizeof(blk), &n);
        if (status == eIO_Timeout)
            continue;
        if (status != eIO_Success  &&  status != eIO_Closed)
            CORE_LOGF(eLOG_Fatal, ("Read error: %s", IO_StatusStr(status)));
        if (n == sizeof(blk))
            CORE_LOGF(eLOG_Warning, ("Line too long, continuing..."));
        if (n) {
            fwrite(blk, 1, n, stdout);
            fputc('\n', stdout);
            fflush(stdout);
        }
    } while (status == eIO_Success  ||  status == eIO_Timeout);

    ConnNetInfo_Destroy(net_info);
    CORE_LOG(eLOG_Note, "Closing connection");
    CONN_Close(conn);

    CORE_LOG(eLOG_Note, "Completed");
    CORE_SetLOG(0);
    return 0;
}