~ubuntu-branches/ubuntu/saucy/ncbi-tools6/saucy-proposed

« back to all changes in this revision

Viewing changes to connect/ncbi_http_connector.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2009-08-11 22:03:47 UTC
  • mfrom: (1.4.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090811220347-g4b6lzdvphvvbpiu
* New upstream release.
* debian/libncbi6.symbols: update accordingly.
* debian/control: clean up obsolete or redundant relationship declarations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ncbi_http_connector.c,v 6.94 2009/02/04 20:49:30 kazimird Exp $
 
1
/* $Id: ncbi_http_connector.c,v 6.96 2009/07/06 18:49:30 kazimird Exp $
2
2
 * ===========================================================================
3
3
 *
4
4
 *                            PUBLIC DOMAIN NOTICE
134
134
            free(*retry);
135
135
        if (read_mode != eRM_DropUnread  &&  uuu->failure_count > 1) {
136
136
            CORE_LOGF_X(1, eLOG_Error,
137
 
                        ("[HTTP]  Too many failed attempts (%d),"
138
 
                         " giving up", uuu->failure_count));
 
137
                        ("[HTTP]  Too many failed attempts (%d), giving up",
 
138
                         uuu->failure_count));
139
139
        }
140
140
        uuu->can_connect = eCC_None;
141
141
        return 0/*failure*/;
148
148
        if (**retry != '?') {
149
149
            if (uuu->net_info->req_method == eReqMethod_Get  ||  !uuu->w_len
150
150
                ||  (uuu->flags & fHCC_InsecureRedirect)) {
151
 
                int/*bool*/secure = uuu->net_info->scheme == eURL_Https ? 1 : 0;
 
151
                int secure = uuu->net_info->scheme == eURL_Https ? 1 : 0;
152
152
                *uuu->net_info->args = '\0'/*arguments not inherited*/;
153
153
                fail = !ConnNetInfo_ParseURL(uuu->net_info, *retry);
154
154
                if (!fail  &&  secure  &&  uuu->net_info->scheme != eURL_Https
161
161
            fail = 1;
162
162
        if (fail) {
163
163
            CORE_LOGF_X(2, eLOG_Error,
164
 
                        ("[HTTP]  %s to redirect to \"%s\"",
165
 
                         fail < 0 ? "Prohibited" : "Unable", *retry));
 
164
                        ("[HTTP]  %s redirect to \"%s\"",
 
165
                         fail < 0 ? "Prohibited" : "Cannot", *retry));
166
166
        }
167
167
        free(*retry);
168
168
        if (fail) {
175
175
                                        uuu->failure_count) == 0) {
176
176
        if (read_mode != eRM_DropUnread  &&  uuu->failure_count > 1) {
177
177
            CORE_LOGF_X(3, eLOG_Error,
178
 
                        ("[HTTP]  Retry attempts (%d) exhausted,"
179
 
                         " giving up", uuu->failure_count));
 
178
                        ("[HTTP]  Retry attempts (%d) exhausted, giving up",
 
179
                         uuu->failure_count));
180
180
        }
181
181
        uuu->can_connect = eCC_None;
182
182
        return 0/*failure*/;
190
190
/* Unconditionally drop the connection; timeout may specify time allowance */
191
191
static void s_DropConnection(SHttpConnector* uuu, const STimeout* timeout)
192
192
{
193
 
    size_t http_size = BUF_Size(uuu->http);
194
193
    assert(uuu->sock);
195
 
    if (http_size  &&  BUF_Read(uuu->http, 0, http_size) != http_size) {
196
 
        CORE_LOG_X(4, eLOG_Error, "[HTTP]  Cannot discard HTTP header buffer");
197
 
        assert(0);
198
 
    }
 
194
    BUF_Erase(uuu->http);
199
195
    SOCK_SetTimeout(uuu->sock, eIO_Close, timeout);
200
196
    SOCK_Close(uuu->sock);
201
197
    uuu->sock = 0;
285
281
    EIO_Status status;
286
282
 
287
283
    for (;;) {
288
 
        char* null = 0;
 
284
        size_t body_size;
 
285
        char   buf[4096];
 
286
        char*  null = 0;
289
287
 
290
288
        if (!uuu->sock) {
291
289
            if ((status = s_Connect(uuu, read_mode)) != eIO_Success)
304
302
 
305
303
            SOCK_SetTimeout(uuu->sock, eIO_Write, uuu->w_timeout);
306
304
            do {
307
 
                char   buf[4096];
308
305
                size_t n_written;
309
306
                size_t n_write = BUF_PeekAt(uuu->w_buf, off, buf, sizeof(buf));
310
307
                status = SOCK_Write(uuu->sock, buf, n_write,
332
329
            break;
333
330
        }
334
331
 
335
 
        if (status == eIO_Timeout  &&  uuu->w_timeout  &&
336
 
            !(uuu->w_timeout->sec | uuu->w_timeout->usec)) {
 
332
        if (status == eIO_Timeout
 
333
            &&  (read_mode == eRM_WaitCalled
 
334
                 ||  (uuu->w_timeout
 
335
                      &&  !(uuu->w_timeout->sec | uuu->w_timeout->usec)))) {
337
336
            break;
338
337
        }
 
338
        if ((body_size = BUF_Size(uuu->w_buf))) {
 
339
            sprintf(buf, "writing request body at offset %lu",
 
340
                    (unsigned long)(body_size - uuu->w_len));
 
341
        } else
 
342
            strcpy(buf, "flushing request header");
339
343
        CORE_LOGF_X(6, eLOG_Error,
340
 
                    ("[HTTP]  Error writing body at offset %lu (%s)",
341
 
                     (unsigned long)(BUF_Size(uuu->w_buf) - uuu->w_len),
342
 
                     IO_StatusStr(status)));
 
344
                    ("[HTTP]  Error %s (%s)", buf, IO_StatusStr(status)));
343
345
 
344
346
        /* write failed; close and try to use another server */
345
347
        SOCK_Abort(uuu->sock);
393
395
                const STimeout* tmo = SOCK_GetTimeout(uuu->sock, eIO_Read);
394
396
                if (!tmo)
395
397
                    level = eLOG_Error;
 
398
                else if (read_mode == eRM_WaitCalled)
 
399
                    return status;
396
400
                else if (tmo->sec | tmo->usec)
397
401
                    level = eLOG_Warning;
398
 
                else if (read_mode != eRM_WaitCalled)
 
402
                else
399
403
                    level = eLOG_Trace;
400
 
                else
401
 
                    return status;
402
404
            } else
403
405
                level = eLOG_Error;
404
406
            CORE_LOGF_X(8, level, ("[HTTP]  Error reading header (%s)",
409
411
    /* the entire header has been read */
410
412
    uuu->read_header = 0/*false*/;
411
413
    status = eIO_Success;
412
 
 
413
 
    if (BUF_Read(uuu->http, 0, size) != size) {
414
 
        CORE_LOG_X(9, eLOG_Error, "[HTTP]  Cannot discard HTTP header buffer");
415
 
        status = eIO_Unknown;
416
 
        assert(0);
417
 
    }
 
414
    BUF_Erase(uuu->http);
418
415
 
419
416
    /* HTTP status must come on the first line of the response */
420
417
    if (sscanf(header, "HTTP/%*d.%*d %d ", &http_status) != 1 || !http_status)
469
466
                if (message != s) {
470
467
                    if (s_MessageHook) {
471
468
                        if (s_MessageIssued <= 0) {
472
 
                            s_MessageIssued = 1;
 
469
                            s_MessageIssued  = 1;
473
470
                            s_MessageHook(message);
474
471
                        }
475
472
                    } else {
636
633
            break;
637
634
 
638
635
        if ((status = s_ReadHeader(uuu, &retry, read_mode)) == eIO_Success) {
639
 
            size_t w_size = BUF_Size(uuu->w_buf);
 
636
            /* pending output data no longer needed */
640
637
            assert(!uuu->read_header  &&  !retry);
641
 
            /* pending output data no longer needed */
642
 
            if (BUF_Read(uuu->w_buf, 0, w_size) != w_size) {
643
 
                CORE_LOG_X(15, eLOG_Error,
644
 
                           "[HTTP]  Cannot discard output buffer");
645
 
                assert(0);
646
 
            }
 
638
            BUF_Erase(uuu->w_buf);
647
639
            break;
648
640
        }
649
641
        /* if polling then bail out with eIO_Timeout */
650
 
        if(status == eIO_Timeout && timeout && !(timeout->sec | timeout->usec))
 
642
        if (status == eIO_Timeout
 
643
            &&  (read_mode == eRM_WaitCalled
 
644
                 ||  (timeout  &&  !(timeout->sec | timeout->usec)))) {
651
645
            break;
 
646
        }
652
647
 
653
648
        /* HTTP header read error; disconnect and try to use another server */
654
649
        SOCK_Abort(uuu->sock);
726
721
{
727
722
    EIO_Status status = eIO_Success;
728
723
 
729
 
    if (read_mode == eRM_DropUnread) {
730
 
        size_t r_size = BUF_Size(uuu->r_buf);
731
 
        if (r_size  &&  BUF_Read(uuu->r_buf, 0, r_size) != r_size) {
732
 
            CORE_LOG_X(17, eLOG_Error, "[HTTP]  Cannot drop input buffer");
733
 
            assert(0);
734
 
        }
735
 
    } else if ((status = s_PreRead(uuu, timeout, read_mode)) == eIO_Success) {
 
724
    if (read_mode == eRM_DropUnread)
 
725
        BUF_Erase(uuu->r_buf);
 
726
    else if ((status = s_PreRead(uuu, timeout, read_mode)) == eIO_Success) {
736
727
        do {
737
728
            char   buf[4096];
738
729
            size_t x_read;
761
752
                                 int/*bool*/     close,
762
753
                                 const STimeout* timeout)
763
754
{
764
 
    size_t w_size;
765
 
 
766
755
    /* store timeouts for later use */
767
756
    if (timeout) {
768
757
        uuu->oo_timeout = *timeout;
783
772
    assert(!uuu->sock);
784
773
 
785
774
    /* clear pending output data, if any */
786
 
    w_size = BUF_Size(uuu->w_buf);
787
 
    if (w_size  &&  BUF_Read(uuu->w_buf, 0, w_size) != w_size) {
788
 
        CORE_LOG_X(18, eLOG_Error, "[HTTP]  Cannot drop output buffer");
789
 
        assert(0);
790
 
    }
 
775
    BUF_Erase(uuu->w_buf);
791
776
}
792
777
 
793
778
 
899
884
 
900
885
    switch (event) {
901
886
    case eIO_Read:
 
887
        if (BUF_Size(uuu->r_buf))
 
888
            return eIO_Success;
902
889
        if (uuu->can_connect == eCC_None)
903
890
            return eIO_Closed;
904
891
        if (!uuu->sock  ||  uuu->read_header) {
905
892
            EIO_Status status = s_PreRead(uuu, timeout, eRM_WaitCalled);
906
 
            if (status != eIO_Success  ||  BUF_Size(uuu->r_buf))
 
893
            if (status != eIO_Success)
907
894
                return status;
908
895
            assert(uuu->sock);
909
896
        }
914
901
            ||  (uuu->sock  &&  uuu->can_connect == eCC_Once)
915
902
            ? eIO_Closed : eIO_Success;
916
903
    default:
917
 
        assert(0);
918
 
        return eIO_InvalidArg;
 
904
        break;
919
905
    }
 
906
    assert(0);
 
907
    return eIO_InvalidArg;
920
908
}
921
909
 
922
910
 
1132
1120
    xxx = net_info ? ConnNetInfo_Clone(net_info) : ConnNetInfo_Create(0);
1133
1121
    if (!xxx)
1134
1122
        return 0;
 
1123
    if ((flags & fHCC_NoAutoRetry)  ||  !xxx->max_try)
 
1124
        xxx->max_try = 1;
1135
1125
 
1136
1126
    if ((xxx->scheme != eURL_Unspec  && 
1137
1127
         xxx->scheme != eURL_Https   &&