~ubuntu-branches/ubuntu/precise/transmission/precise

« back to all changes in this revision

Viewing changes to third-party/libevent/http.c

  • Committer: Bazaar Package Importer
  • Author(s): Leo Costela
  • Date: 2009-05-17 19:39:51 UTC
  • mto: (1.3.4 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: james.westby@ubuntu.com-20090517193951-k8x15sqoxzf7cuyx
ImportĀ upstreamĀ versionĀ 1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
#include "http-internal.h"
91
91
 
92
92
#ifdef WIN32
93
 
#define snprintf _snprintf
94
93
#define strcasecmp _stricmp
95
94
#define strncasecmp _strnicmp
96
95
#define strdup _strdup
97
96
#endif
98
97
 
 
98
#ifndef HAVE_GETNAMEINFO
 
99
#define NI_MAXSERV 32
 
100
#define NI_MAXHOST 1025
 
101
 
 
102
#define NI_NUMERICHOST 1
 
103
#define NI_NUMERICSERV 2
 
104
 
 
105
int
 
106
fake_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, 
 
107
        size_t hostlen, char *serv, size_t servlen, int flags)
 
108
{
 
109
        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
 
110
        
 
111
        if (serv != NULL) {
 
112
                                char tmpserv[16];
 
113
                                evutil_snprintf(tmpserv, sizeof(tmpserv),
 
114
                                        "%d", ntohs(sin->sin_port));
 
115
                if (strlcpy(serv, tmpserv, servlen) >= servlen)
 
116
                        return (-1);
 
117
        }
 
118
 
 
119
        if (host != NULL) {
 
120
                if (flags & NI_NUMERICHOST) {
 
121
                        if (strlcpy(host, inet_ntoa(sin->sin_addr),
 
122
                            hostlen) >= hostlen)
 
123
                                return (-1);
 
124
                        else
 
125
                                return (0);
 
126
                } else {
 
127
                                                struct hostent *hp;
 
128
                        hp = gethostbyaddr((char *)&sin->sin_addr, 
 
129
                            sizeof(struct in_addr), AF_INET);
 
130
                        if (hp == NULL)
 
131
                                return (-2);
 
132
                        
 
133
                        if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
 
134
                                return (-1);
 
135
                        else
 
136
                                return (0);
 
137
                }
 
138
        }
 
139
        return (0);
 
140
}
 
141
 
 
142
#endif
 
143
 
99
144
#ifndef HAVE_GETADDRINFO
100
145
struct addrinfo {
101
146
        int ai_family;
152
197
extern int debug;
153
198
 
154
199
static int socket_connect(int fd, const char *address, unsigned short port);
155
 
static int bind_socket_ai(struct addrinfo *);
156
 
static int bind_socket(const char *, u_short);
 
200
static int bind_socket_ai(struct addrinfo *, int reuse);
 
201
static int bind_socket(const char *, u_short, int reuse);
157
202
static void name_from_addr(struct sockaddr *, socklen_t, char **, char **);
158
203
static int evhttp_associate_new_request_with_connection(
159
204
        struct evhttp_connection *evcon);
162
207
static void evhttp_connection_stop_detectclose(
163
208
        struct evhttp_connection *evcon);
164
209
static void evhttp_request_dispatch(struct evhttp_connection* evcon);
 
210
static void evhttp_read_firstline(struct evhttp_connection *evcon,
 
211
                                  struct evhttp_request *req);
 
212
static void evhttp_read_header(struct evhttp_connection *evcon,
 
213
    struct evhttp_request *req);
 
214
static int evhttp_add_header_internal(struct evkeyvalq *headers,
 
215
    const char *key, const char *value);
 
216
static int evhttp_decode_uri_internal(const char *uri, size_t length,
 
217
    char *ret, int always_decode_plus);
165
218
 
166
219
void evhttp_read(int, short, void *);
167
220
void evhttp_write(int, short, void *);
300
353
        evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_WRITE_TIMEOUT);
301
354
}
302
355
 
 
356
static int
 
357
evhttp_connected(struct evhttp_connection *evcon)
 
358
{
 
359
        switch (evcon->state) {
 
360
        case EVCON_DISCONNECTED:
 
361
        case EVCON_CONNECTING:
 
362
                return (0);
 
363
        case EVCON_IDLE:
 
364
        case EVCON_READING_FIRSTLINE:
 
365
        case EVCON_READING_HEADERS:
 
366
        case EVCON_READING_BODY:
 
367
        case EVCON_READING_TRAILER:
 
368
        case EVCON_WRITING:
 
369
        default:
 
370
                return (1);
 
371
        }
 
372
}
 
373
 
303
374
/*
304
 
 * Create the headers need for an HTTP request
 
375
 * Create the headers needed for an HTTP request
305
376
 */
306
377
static void
307
378
evhttp_make_header_request(struct evhttp_connection *evcon,
308
379
    struct evhttp_request *req)
309
380
{
310
 
        char line[1024];
311
381
        const char *method;
312
382
        
313
 
        evhttp_remove_header(req->output_headers, "Accept-Encoding");
314
383
        evhttp_remove_header(req->output_headers, "Proxy-Connection");
315
384
 
316
385
        /* Generate request line */
317
386
        method = evhttp_method(req->type);
318
 
        snprintf(line, sizeof(line), "%s %s HTTP/%d.%d\r\n",
 
387
        evbuffer_add_printf(evcon->output_buffer, "%s %s HTTP/%d.%d\r\n",
319
388
            method, req->uri, req->major, req->minor);
320
 
        evbuffer_add(evcon->output_buffer, line, strlen(line));
321
389
 
322
390
        /* Add the content length on a post request if missing */
323
391
        if (req->type == EVHTTP_REQ_POST &&
324
392
            evhttp_find_header(req->output_headers, "Content-Length") == NULL){
325
393
                char size[12];
326
 
                snprintf(size, sizeof(size), "%ld",
327
 
                         (long)EVBUFFER_LENGTH(req->output_buffer));
 
394
                evutil_snprintf(size, sizeof(size), "%ld",
 
395
                    (long)EVBUFFER_LENGTH(req->output_buffer));
328
396
                evhttp_add_header(req->output_headers, "Content-Length", size);
329
397
        }
330
398
}
380
448
        if (evhttp_find_header(headers, "Transfer-Encoding") == NULL &&
381
449
            evhttp_find_header(headers, "Content-Length") == NULL) {
382
450
                char len[12];
383
 
                snprintf(len, sizeof(len), "%ld", content_length);
 
451
                evutil_snprintf(len, sizeof(len), "%ld", content_length);
384
452
                evhttp_add_header(headers, "Content-Length", len);
385
453
        }
386
454
}
394
462
    struct evhttp_request *req)
395
463
{
396
464
        int is_keepalive = evhttp_is_connection_keepalive(req->input_headers);
397
 
        char line[1024];
398
 
        snprintf(line, sizeof(line), "HTTP/%d.%d %d %s\r\n",
 
465
        evbuffer_add_printf(evcon->output_buffer, "HTTP/%d.%d %d %s\r\n",
399
466
            req->major, req->minor, req->response_code,
400
467
            req->response_code_line);
401
 
        evbuffer_add(evcon->output_buffer, line, strlen(line));
402
468
 
403
469
        if (req->major == 1) {
404
470
                if (req->minor == 1)
445
511
void
446
512
evhttp_make_header(struct evhttp_connection *evcon, struct evhttp_request *req)
447
513
{
448
 
        char line[1024];
449
514
        struct evkeyval *header;
450
515
 
451
516
        /*
459
524
        }
460
525
 
461
526
        TAILQ_FOREACH(header, req->output_headers, next) {
462
 
                snprintf(line, sizeof(line), "%s: %s\r\n",
 
527
                evbuffer_add_printf(evcon->output_buffer, "%s: %s\r\n",
463
528
                    header->key, header->value);
464
 
                evbuffer_add(evcon->output_buffer, line, strlen(line));
465
529
        }
466
530
        evbuffer_add(evcon->output_buffer, "\r\n", 2);
467
531
 
508
572
                /* Generate request file */
509
573
                if (p2 == NULL)
510
574
                        p2 = "";
511
 
                snprintf(file, sizeof(file), "/%s", p2);
 
575
                evutil_snprintf(file, sizeof(file), "/%s", p2);
512
576
        }
513
577
 
514
578
        p = strchr(host, ':');
642
706
                (*evcon->cb)(evcon, evcon->cb_arg);
643
707
}
644
708
 
 
709
/**
 
710
 * Advance the connection state.
 
711
 * - If this is an outgoing connection, we've just processed the response;
 
712
 *   idle or close the connection.
 
713
 * - If this is an incoming connection, we've just processed the request;
 
714
 *   respond.
 
715
 */
645
716
static void
646
717
evhttp_connection_done(struct evhttp_connection *evcon)
647
718
{
648
719
        struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
649
720
        int con_outgoing = evcon->flags & EVHTTP_CON_OUTGOING;
650
721
 
651
 
        /*
652
 
         * if this is an incoming connection, we need to leave the request
653
 
         * on the connection, so that we can reply to it.
654
 
         */
655
722
        if (con_outgoing) {
 
723
                /* idle or close the connection */
656
724
                int need_close;
657
725
                TAILQ_REMOVE(&evcon->requests, req, next);
658
726
                req->evcon = NULL;
659
727
 
 
728
                evcon->state = EVCON_IDLE;
 
729
 
660
730
                need_close = 
661
 
                    evhttp_is_connection_close(req->flags, req->input_headers) ||
 
731
                    evhttp_is_connection_close(req->flags, req->input_headers)||
662
732
                    evhttp_is_connection_close(req->flags, req->output_headers);
663
733
 
664
734
                /* check if we got asked to close the connection */
668
738
                if (TAILQ_FIRST(&evcon->requests) != NULL) {
669
739
                        /*
670
740
                         * We have more requests; reset the connection
671
 
                         * and deal with the next request.  xxx: no
672
 
                         * persistent connection right now
 
741
                         * and deal with the next request.
673
742
                         */
674
 
                        if (evcon->state != EVCON_CONNECTED)
 
743
                        if (!evhttp_connected(evcon))
675
744
                                evhttp_connection_connect(evcon);
676
745
                        else
677
746
                                evhttp_request_dispatch(evcon);
682
751
                         */
683
752
                        evhttp_connection_start_detectclose(evcon);
684
753
                }
 
754
        } else {
 
755
                /*
 
756
                 * incoming connection - we need to leave the request on the
 
757
                 * connection so that we can reply to it.
 
758
                 */
 
759
                evcon->state = EVCON_WRITING;
685
760
        }
686
761
 
687
762
        /* notify the user of the request */
695
770
 
696
771
/*
697
772
 * Handles reading from a chunked request.
698
 
 * return 1: all data has been read
699
 
 * return 0: more data is expected
700
 
 * return -1: data is corrupted
 
773
 *   return ALL_DATA_READ:
 
774
 *     all data has been read
 
775
 *   return MORE_DATA_EXPECTED:
 
776
 *     more data is expected
 
777
 *   return DATA_CORRUPTED:
 
778
 *     data is corrupted
 
779
 *   return REQUEST_CANCLED:
 
780
 *     request was canceled by the user calling evhttp_cancel_request
701
781
 */
702
782
 
703
 
static int
 
783
static enum message_read_status
704
784
evhttp_handle_chunked_read(struct evhttp_request *req, struct evbuffer *buf)
705
785
{
706
786
        int len;
708
788
        while ((len = EVBUFFER_LENGTH(buf)) > 0) {
709
789
                if (req->ntoread < 0) {
710
790
                        /* Read chunk size */
 
791
                        ev_int64_t ntoread;
711
792
                        char *p = evbuffer_readline(buf);
712
793
                        char *endp;
713
794
                        int error;
718
799
                                free(p);
719
800
                                continue;
720
801
                        }
721
 
                        req->ntoread = evutil_strtoll(p, &endp, 16);
722
 
                        error = *p == '\0' || (*endp != '\0' && *endp != ' ');
 
802
                        ntoread = evutil_strtoll(p, &endp, 16);
 
803
                        error = (*p == '\0' ||
 
804
                            (*endp != '\0' && *endp != ' ') ||
 
805
                            ntoread < 0);
723
806
                        free(p);
724
807
                        if (error) {
725
808
                                /* could not get chunk size */
726
 
                                return (-1);
 
809
                                return (DATA_CORRUPTED);
727
810
                        }
 
811
                        req->ntoread = ntoread;
728
812
                        if (req->ntoread == 0) {
729
813
                                /* Last chunk */
730
 
                                return (1);
 
814
                                return (ALL_DATA_READ);
731
815
                        }
732
816
                        continue;
733
817
                }
734
818
 
735
819
                /* don't have enough to complete a chunk; wait for more */
736
820
                if (len < req->ntoread)
737
 
                        return (0);
 
821
                        return (MORE_DATA_EXPECTED);
738
822
 
739
823
                /* Completed chunk */
740
824
                evbuffer_add(req->input_buffer,
748
832
                }
749
833
        }
750
834
 
751
 
        return (0);
 
835
        return (MORE_DATA_EXPECTED);
 
836
}
 
837
 
 
838
static void
 
839
evhttp_read_trailer(struct evhttp_connection *evcon, struct evhttp_request *req)
 
840
{
 
841
        struct evbuffer *buf = evcon->input_buffer;
 
842
 
 
843
        switch (evhttp_parse_headers(req, buf)) {
 
844
        case DATA_CORRUPTED:
 
845
                evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
 
846
                break;
 
847
        case ALL_DATA_READ:
 
848
                event_del(&evcon->ev);
 
849
                evhttp_connection_done(evcon);
 
850
                break;
 
851
        case MORE_DATA_EXPECTED:
 
852
        default:
 
853
                evhttp_add_event(&evcon->ev, evcon->timeout,
 
854
                    HTTP_READ_TIMEOUT);
 
855
                break;
 
856
        }
752
857
}
753
858
 
754
859
static void
757
862
        struct evbuffer *buf = evcon->input_buffer;
758
863
        
759
864
        if (req->chunked) {
760
 
                int res = evhttp_handle_chunked_read(req, buf);
761
 
                if (res == 1) {
 
865
                switch (evhttp_handle_chunked_read(req, buf)) {
 
866
                case ALL_DATA_READ:
762
867
                        /* finished last chunk */
763
 
                        evhttp_connection_done(evcon);
 
868
                        evcon->state = EVCON_READING_TRAILER;
 
869
                        evhttp_read_trailer(evcon, req);
764
870
                        return;
765
 
                } else if (res == -1) {
 
871
                case DATA_CORRUPTED:
766
872
                        /* corrupted data */
767
873
                        evhttp_connection_fail(evcon,
768
874
                            EVCON_HTTP_INVALID_HEADER);
769
875
                        return;
 
876
                case REQUEST_CANCELED:
 
877
                        /* request canceled */
 
878
                        evhttp_request_free(req);
 
879
                        return;
 
880
                case MORE_DATA_EXPECTED:
 
881
                default:
 
882
                        break;
770
883
                }
771
884
        } else if (req->ntoread < 0) {
772
885
                /* Read until connection close. */
810
923
        event_debug(("%s: got %d on %d\n", __func__, n, fd));
811
924
        
812
925
        if (n == -1) {
813
 
                event_debug(("%s: evbuffer_read", __func__));
814
 
                evhttp_connection_fail(evcon, EVCON_HTTP_EOF);
 
926
                if (errno != EINTR && errno != EAGAIN) {
 
927
                        event_debug(("%s: evbuffer_read", __func__));
 
928
                        evhttp_connection_fail(evcon, EVCON_HTTP_EOF);
 
929
                } else {
 
930
                        evhttp_add_event(&evcon->ev, evcon->timeout,
 
931
                            HTTP_READ_TIMEOUT);        
 
932
                }
815
933
                return;
816
934
        } else if (n == 0) {
817
935
                /* Connection closed */
818
936
                evhttp_connection_done(evcon);
819
937
                return;
820
938
        }
821
 
        evhttp_read_body(evcon, req);
 
939
 
 
940
        switch (evcon->state) {
 
941
        case EVCON_READING_FIRSTLINE:
 
942
                evhttp_read_firstline(evcon, req);
 
943
                break;
 
944
        case EVCON_READING_HEADERS:
 
945
                evhttp_read_header(evcon, req);
 
946
                break;
 
947
        case EVCON_READING_BODY:
 
948
                evhttp_read_body(evcon, req);
 
949
                break;
 
950
        case EVCON_READING_TRAILER:
 
951
                evhttp_read_trailer(evcon, req);
 
952
                break;
 
953
        case EVCON_DISCONNECTED:
 
954
        case EVCON_CONNECTING:
 
955
        case EVCON_IDLE:
 
956
        case EVCON_WRITING:
 
957
        default:
 
958
                event_errx(1, "%s: illegal connection state %d",
 
959
                           __func__, evcon->state);
 
960
        }
822
961
}
823
962
 
824
963
static void
828
967
        struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
829
968
        assert(req != NULL);
830
969
 
 
970
        assert(evcon->state == EVCON_WRITING);
 
971
 
831
972
        /* We are done writing our header and are now expecting the response */
832
973
        req->kind = EVHTTP_RESPONSE;
833
974
 
845
986
 
846
987
        /* notify interested parties that this connection is going down */
847
988
        if (evcon->fd != -1) {
848
 
                if (evcon->state == EVCON_CONNECTED && evcon->closecb != NULL)
 
989
                if (evhttp_connected(evcon) && evcon->closecb != NULL)
849
990
                        (*evcon->closecb)(evcon, evcon->closecb_arg);
850
991
        }
851
992
 
895
1036
                event_err(1, "%s: strdup", __func__);
896
1037
}
897
1038
 
 
1039
void
 
1040
evhttp_connection_set_local_port(struct evhttp_connection *evcon,
 
1041
    unsigned short port)
 
1042
{
 
1043
        assert(evcon->state == EVCON_DISCONNECTED);
 
1044
        evcon->bind_port = port;
 
1045
}
898
1046
 
899
1047
static void
900
1048
evhttp_request_dispatch(struct evhttp_connection* evcon)
909
1057
        evhttp_connection_stop_detectclose(evcon);
910
1058
        
911
1059
        /* we assume that the connection is connected already */
912
 
        assert(evcon->state == EVCON_CONNECTED);
 
1060
        assert(evcon->state == EVCON_IDLE);
 
1061
 
 
1062
        evcon->state = EVCON_WRITING;
913
1063
 
914
1064
        /* Create the header from the store arguments */
915
1065
        evhttp_make_header(evcon, req);
926
1076
 
927
1077
        if (evcon->fd != -1) {
928
1078
                /* inform interested parties about connection close */
929
 
                if (evcon->state == EVCON_CONNECTED && evcon->closecb != NULL)
 
1079
                if (evhttp_connected(evcon) && evcon->closecb != NULL)
930
1080
                        (*evcon->closecb)(evcon, evcon->closecb_arg);
931
1081
 
932
1082
                EVUTIL_CLOSESOCKET(evcon->fd);
934
1084
        }
935
1085
        evcon->state = EVCON_DISCONNECTED;
936
1086
 
937
 
        /* remove unneeded flags */
938
 
        evcon->flags &= ~EVHTTP_CON_CLOSEDETECT;
 
1087
        evbuffer_drain(evcon->input_buffer,
 
1088
            EVBUFFER_LENGTH(evcon->input_buffer));
 
1089
        evbuffer_drain(evcon->output_buffer,
 
1090
            EVBUFFER_LENGTH(evcon->output_buffer));
939
1091
}
940
1092
 
941
1093
static void
1012
1164
 
1013
1165
        /* Reset the retry count as we were successful in connecting */
1014
1166
        evcon->retry_cnt = 0;
1015
 
        evcon->state = EVCON_CONNECTED;
 
1167
        evcon->state = EVCON_IDLE;
1016
1168
 
1017
1169
        /* try to start requests that have queued up on this connection */
1018
1170
        evhttp_request_dispatch(evcon);
1208
1360
        return (0);
1209
1361
}
1210
1362
 
 
1363
static int
 
1364
evhttp_header_is_valid_value(const char *value)
 
1365
{
 
1366
        const char *p = value;
 
1367
 
 
1368
        while ((p = strpbrk(p, "\r\n")) != NULL) {
 
1369
                /* we really expect only one new line */
 
1370
                p += strspn(p, "\r\n");
 
1371
                /* we expect a space or tab for continuation */
 
1372
                if (*p != ' ' && *p != '\t')
 
1373
                        return (0);
 
1374
        }
 
1375
        return (1);
 
1376
}
 
1377
 
1211
1378
int
1212
1379
evhttp_add_header(struct evkeyvalq *headers,
1213
1380
    const char *key, const char *value)
1214
1381
{
1215
 
        struct evkeyval *header = NULL;
1216
 
 
1217
1382
        event_debug(("%s: key: %s val: %s\n", __func__, key, value));
1218
1383
 
1219
 
        if (strchr(value, '\r') != NULL || strchr(value, '\n') != NULL ||
1220
 
            strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
 
1384
        if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
1221
1385
                /* drop illegal headers */
1222
 
                event_debug(("%s: dropping illegal header\n", __func__));
1223
 
                return (-1);
1224
 
        }
1225
 
 
1226
 
        header = calloc(1, sizeof(struct evkeyval));
 
1386
                event_debug(("%s: dropping illegal header key\n", __func__));
 
1387
                return (-1);
 
1388
        }
 
1389
        
 
1390
        if (!evhttp_header_is_valid_value(value)) {
 
1391
                event_debug(("%s: dropping illegal header value\n", __func__));
 
1392
                return (-1);
 
1393
        }
 
1394
 
 
1395
        return (evhttp_add_header_internal(headers, key, value));
 
1396
}
 
1397
 
 
1398
static int
 
1399
evhttp_add_header_internal(struct evkeyvalq *headers,
 
1400
    const char *key, const char *value)
 
1401
{
 
1402
        struct evkeyval *header = calloc(1, sizeof(struct evkeyval));
1227
1403
        if (header == NULL) {
1228
1404
                event_warn("%s: calloc", __func__);
1229
1405
                return (-1);
1250
1426
 * request object given an event buffer.
1251
1427
 *
1252
1428
 * Returns
1253
 
 *   -1  on error
1254
 
 *    0  when we need to read more headers
1255
 
 *    1  when all headers have been read.
 
1429
 *   DATA_CORRUPTED      on error
 
1430
 *   MORE_DATA_EXPECTED  when we need to read more headers
 
1431
 *   ALL_DATA_READ       when all headers have been read.
1256
1432
 */
1257
1433
 
1258
 
int
1259
 
evhttp_parse_lines(struct evhttp_request *req, struct evbuffer* buffer)
1260
 
{
1261
 
        char *line;
1262
 
        int done = 0;
 
1434
enum message_read_status
 
1435
evhttp_parse_firstline(struct evhttp_request *req, struct evbuffer *buffer)
 
1436
{
 
1437
        char *line;
 
1438
        enum message_read_status status = ALL_DATA_READ;
 
1439
 
 
1440
        line = evbuffer_readline(buffer);
 
1441
        if (line == NULL)
 
1442
                return (MORE_DATA_EXPECTED);
 
1443
 
 
1444
        switch (req->kind) {
 
1445
        case EVHTTP_REQUEST:
 
1446
                if (evhttp_parse_request_line(req, line) == -1)
 
1447
                        status = DATA_CORRUPTED;
 
1448
                break;
 
1449
        case EVHTTP_RESPONSE:
 
1450
                if (evhttp_parse_response_line(req, line) == -1)
 
1451
                        status = DATA_CORRUPTED;
 
1452
                break;
 
1453
        default:
 
1454
                status = DATA_CORRUPTED;
 
1455
        }
 
1456
 
 
1457
        free(line);
 
1458
        return (status);
 
1459
}
 
1460
 
 
1461
static int
 
1462
evhttp_append_to_last_header(struct evkeyvalq *headers, const char *line)
 
1463
{
 
1464
        struct evkeyval *header = TAILQ_LAST(headers, evkeyvalq);
 
1465
        char *newval;
 
1466
        size_t old_len, line_len;
 
1467
 
 
1468
        if (header == NULL)
 
1469
                return (-1);
 
1470
 
 
1471
        old_len = strlen(header->value);
 
1472
        line_len = strlen(line);
 
1473
 
 
1474
        newval = realloc(header->value, old_len + line_len + 1);
 
1475
        if (newval == NULL)
 
1476
                return (-1);
 
1477
 
 
1478
        memcpy(newval + old_len, line, line_len + 1);
 
1479
        header->value = newval;
 
1480
 
 
1481
        return (0);
 
1482
}
 
1483
 
 
1484
enum message_read_status
 
1485
evhttp_parse_headers(struct evhttp_request *req, struct evbuffer* buffer)
 
1486
{
 
1487
        char *line;
 
1488
        enum message_read_status status = MORE_DATA_EXPECTED;
1263
1489
 
1264
1490
        struct evkeyvalq* headers = req->input_headers;
1265
 
        while ((line = evbuffer_readline(buffer)) != NULL) {
 
1491
        while ((line = evbuffer_readline(buffer))
 
1492
               != NULL) {
1266
1493
                char *skey, *svalue;
1267
1494
 
1268
1495
                if (*line == '\0') { /* Last header - Done */
1269
 
                        done = 1;
1270
 
                        free (line);
 
1496
                        status = ALL_DATA_READ;
 
1497
                        free(line);
1271
1498
                        break;
1272
1499
                }
1273
1500
 
 
1501
                /* Check if this is a continuation line */
 
1502
                if (*line == ' ' || *line == '\t') {
 
1503
                        if (evhttp_append_to_last_header(headers, line) == -1)
 
1504
                                goto error;
 
1505
                        free(line);
 
1506
                        continue;
 
1507
                }
 
1508
 
1274
1509
                /* Processing of header lines */
1275
 
                if (req->got_firstline == 0) {
1276
 
                        switch (req->kind) {
1277
 
                        case EVHTTP_REQUEST:
1278
 
                                if (evhttp_parse_request_line(req, line) == -1)
1279
 
                                        goto error;
1280
 
                                break;
1281
 
                        case EVHTTP_RESPONSE:
1282
 
                                if (evhttp_parse_response_line(req, line) == -1)
1283
 
                                        goto error;
1284
 
                                break;
1285
 
                        default:
1286
 
                                goto error;
1287
 
                        }
1288
 
                        req->got_firstline = 1;
1289
 
                } else {
1290
 
                        /* Regular header */
1291
 
                        svalue = line;
1292
 
                        skey = strsep(&svalue, ":");
1293
 
                        if (svalue == NULL)
1294
 
                                goto error;
1295
 
 
1296
 
                        svalue += strspn(svalue, " ");
1297
 
 
1298
 
                        if (evhttp_add_header(headers, skey, svalue) == -1)
1299
 
                                goto error;
1300
 
                }
1301
 
 
1302
 
                free (line);
 
1510
                svalue = line;
 
1511
                skey = strsep(&svalue, ":");
 
1512
                if (svalue == NULL)
 
1513
                        goto error;
 
1514
 
 
1515
                svalue += strspn(svalue, " ");
 
1516
 
 
1517
                if (evhttp_add_header(headers, skey, svalue) == -1)
 
1518
                        goto error;
 
1519
 
 
1520
                free(line);
1303
1521
        }
1304
1522
 
1305
 
        return (done);
 
1523
        return (status);
1306
1524
 
1307
1525
 error:
1308
 
        free (line);
1309
 
        return (-1);
 
1526
        free(line);
 
1527
        return (DATA_CORRUPTED);
1310
1528
}
1311
1529
 
1312
1530
static int
1332
1550
                req->ntoread = -1;
1333
1551
        } else {
1334
1552
                char *endp;
1335
 
                req->ntoread = evutil_strtoll(content_length, &endp, 10);
1336
 
                if (*content_length == '\0' || *endp != '\0') {
1337
 
                        event_warnx("%s: illegal content length: %s",
1338
 
                            __func__, content_length);
 
1553
                ev_int64_t ntoread = evutil_strtoll(content_length, &endp, 10);
 
1554
                if (*content_length == '\0' || *endp != '\0' || ntoread < 0) {
 
1555
                        event_debug(("%s: illegal content length: %s",
 
1556
                                __func__, content_length));
1339
1557
                        return (-1);
1340
1558
                }
 
1559
                req->ntoread = ntoread;
1341
1560
        }
1342
1561
                
1343
 
        event_debug(("%s: bytes to read: %d (in buffer %d)\n",
 
1562
        event_debug(("%s: bytes to read: %lld (in buffer %ld)\n",
1344
1563
                __func__, req->ntoread,
1345
1564
                EVBUFFER_LENGTH(req->evcon->input_buffer)));
1346
1565
 
1357
1576
                evhttp_connection_done(evcon);
1358
1577
                return;
1359
1578
        }
 
1579
        evcon->state = EVCON_READING_BODY;
1360
1580
        xfer_enc = evhttp_find_header(req->input_headers, "Transfer-Encoding");
1361
1581
        if (xfer_enc != NULL && strcasecmp(xfer_enc, "chunked") == 0) {
1362
1582
                req->chunked = 1;
1371
1591
        evhttp_read_body(evcon, req);
1372
1592
}
1373
1593
 
1374
 
void
1375
 
evhttp_read_header(int fd, short what, void *arg)
1376
 
{
1377
 
        struct evhttp_connection *evcon = arg;
1378
 
        struct evhttp_request *req = TAILQ_FIRST(&evcon->requests);
1379
 
        int n, res;
1380
 
 
1381
 
        if (what == EV_TIMEOUT) {
1382
 
                event_debug(("%s: timeout on %d\n", __func__, fd));
1383
 
                evhttp_connection_fail(evcon, EVCON_HTTP_TIMEOUT);
1384
 
                return;
1385
 
        }
1386
 
 
1387
 
        n = evbuffer_read(evcon->input_buffer, fd, -1);
1388
 
        if (n == 0) {
1389
 
                event_debug(("%s: no more data on %d", __func__, fd));
1390
 
                evhttp_connection_fail(evcon, EVCON_HTTP_EOF);
1391
 
                return;
1392
 
        }
1393
 
        if (n == -1) {
1394
 
                event_debug(("%s: bad read on %d", __func__, fd));
1395
 
                evhttp_connection_fail(evcon, EVCON_HTTP_EOF);
1396
 
                return;
1397
 
        }
1398
 
 
1399
 
        res = evhttp_parse_lines(req, evcon->input_buffer);
1400
 
        if (res == -1) {
 
1594
static void
 
1595
evhttp_read_firstline(struct evhttp_connection *evcon,
 
1596
                      struct evhttp_request *req)
 
1597
{
 
1598
        enum message_read_status res;
 
1599
 
 
1600
        res = evhttp_parse_firstline(req, evcon->input_buffer);
 
1601
        if (res == DATA_CORRUPTED) {
 
1602
                /* Error while reading, terminate */
 
1603
                event_debug(("%s: bad header lines on %d\n",
 
1604
                        __func__, evcon->fd));
 
1605
                evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
 
1606
                return;
 
1607
        } else if (res == MORE_DATA_EXPECTED) {
 
1608
                /* Need more header lines */
 
1609
                evhttp_add_event(&evcon->ev, 
 
1610
                    evcon->timeout, HTTP_READ_TIMEOUT);
 
1611
                return;
 
1612
        }
 
1613
 
 
1614
        evcon->state = EVCON_READING_HEADERS;
 
1615
        evhttp_read_header(evcon, req);
 
1616
}
 
1617
 
 
1618
static void
 
1619
evhttp_read_header(struct evhttp_connection *evcon, struct evhttp_request *req)
 
1620
{
 
1621
        enum message_read_status res;
 
1622
        int fd = evcon->fd;
 
1623
 
 
1624
        res = evhttp_parse_headers(req, evcon->input_buffer);
 
1625
        if (res == DATA_CORRUPTED) {
1401
1626
                /* Error while reading, terminate */
1402
1627
                event_debug(("%s: bad header lines on %d\n", __func__, fd));
1403
1628
                evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER);
1404
1629
                return;
1405
 
        } else if (res == 0) {
 
1630
        } else if (res == MORE_DATA_EXPECTED) {
1406
1631
                /* Need more header lines */
1407
1632
                evhttp_add_event(&evcon->ev, 
1408
1633
                    evcon->timeout, HTTP_READ_TIMEOUT);
1541
1766
        assert(!(evcon->flags & EVHTTP_CON_INCOMING));
1542
1767
        evcon->flags |= EVHTTP_CON_OUTGOING;
1543
1768
        
1544
 
        evcon->fd = bind_socket(evcon->bind_address, 0);
 
1769
        evcon->fd = bind_socket(
 
1770
                evcon->bind_address, evcon->bind_port, 0 /*reuse*/);
1545
1771
        if (evcon->fd == -1) {
1546
1772
                event_debug(("%s: failed to bind to \"%s\"",
1547
1773
                        __func__, evcon->bind_address));
1595
1821
        TAILQ_INSERT_TAIL(&evcon->requests, req, next);
1596
1822
 
1597
1823
        /* If the connection object is not connected; make it so */
1598
 
        if (evcon->state != EVCON_CONNECTED)
 
1824
        if (!evhttp_connected(evcon))
1599
1825
                return (evhttp_connection_connect(evcon));
1600
1826
 
1601
1827
        /*
1620
1846
        /* Set up an event to read the headers */
1621
1847
        if (event_initialized(&evcon->ev))
1622
1848
                event_del(&evcon->ev);
1623
 
        event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read_header, evcon);
 
1849
        event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read, evcon);
1624
1850
        EVHTTP_BASE_SET(evcon, &evcon->ev);
1625
1851
        
1626
1852
        evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT);
 
1853
        evcon->state = EVCON_READING_FIRSTLINE;
1627
1854
}
1628
1855
 
1629
1856
static void
1707
1934
evhttp_send_reply(struct evhttp_request *req, int code, const char *reason,
1708
1935
    struct evbuffer *databuf)
1709
1936
{
1710
 
        /* set up to watch for client close */
1711
 
        evhttp_connection_start_detectclose(req->evcon);
1712
1937
        evhttp_response_code(req, code, reason);
1713
1938
        
1714
1939
        evhttp_send(req, databuf);
1718
1943
evhttp_send_reply_start(struct evhttp_request *req, int code,
1719
1944
    const char *reason)
1720
1945
{
1721
 
        /* set up to watch for client close */
1722
 
        evhttp_connection_start_detectclose(req->evcon);
1723
1946
        evhttp_response_code(req, code, reason);
1724
1947
        if (req->major == 1 && req->minor == 1) {
1725
1948
                /* use chunked encoding for HTTP/1.1 */
1838
2061
        return (p);
1839
2062
}
1840
2063
 
1841
 
char *
1842
 
evhttp_decode_uri(const char *uri)
 
2064
/*
 
2065
 * @param always_decode_plus: when true we transform plus to space even
 
2066
 *     if we have not seen a ?.
 
2067
 */
 
2068
static int
 
2069
evhttp_decode_uri_internal(
 
2070
        const char *uri, size_t length, char *ret, int always_decode_plus)
1843
2071
{
1844
 
        char c, *ret;
1845
 
        int i, j, in_query = 0;
1846
 
        
1847
 
        ret = malloc(strlen(uri) + 1);
1848
 
        if (ret == NULL)
1849
 
                event_err(1, "%s: malloc(%lu)", __func__,
1850
 
                          (unsigned long)(strlen(uri) + 1));
 
2072
        char c;
 
2073
        int i, j, in_query = always_decode_plus;
1851
2074
        
1852
2075
        for (i = j = 0; uri[i] != '\0'; i++) {
1853
2076
                c = uri[i];
1864
2087
                ret[j++] = c;
1865
2088
        }
1866
2089
        ret[j] = '\0';
1867
 
        
 
2090
 
 
2091
        return (j);
 
2092
}
 
2093
 
 
2094
char *
 
2095
evhttp_decode_uri(const char *uri)
 
2096
{
 
2097
        char *ret;
 
2098
 
 
2099
        if ((ret = malloc(strlen(uri) + 1)) == NULL)
 
2100
                event_err(1, "%s: malloc(%lu)", __func__,
 
2101
                          (unsigned long)(strlen(uri) + 1));
 
2102
 
 
2103
        evhttp_decode_uri_internal(uri, strlen(uri),
 
2104
            ret, 0 /*always_decode_plus*/);
 
2105
 
1868
2106
        return (ret);
1869
2107
}
1870
2108
 
1871
2109
/* 
1872
2110
 * Helper function to parse out arguments in a query.
1873
2111
 * The arguments are separated by key and value.
1874
 
 * URI should already be decoded.
1875
2112
 */
1876
2113
 
1877
2114
void
1898
2135
 
1899
2136
        p = argument;
1900
2137
        while (p != NULL && *p != '\0') {
1901
 
                char *key, *value;
 
2138
                char *key, *value, *decoded_value;
1902
2139
                argument = strsep(&p, "&");
1903
2140
 
1904
2141
                value = argument;
1906
2143
                if (value == NULL)
1907
2144
                        goto error;
1908
2145
 
1909
 
                value = evhttp_decode_uri(value);
1910
 
                event_debug(("Query Param: %s -> %s\n", key, value));
1911
 
                evhttp_add_header(headers, key, value);
1912
 
                free(value);
 
2146
                if ((decoded_value = malloc(strlen(value) + 1)) == NULL)
 
2147
                        event_err(1, "%s: malloc", __func__);
 
2148
 
 
2149
                evhttp_decode_uri_internal(value, strlen(value),
 
2150
                    decoded_value, 1 /*always_decode_plus*/);
 
2151
                event_debug(("Query Param: %s -> %s\n", key, decoded_value));
 
2152
                evhttp_add_header_internal(headers, key, decoded_value);
 
2153
                free(decoded_value);
1913
2154
        }
1914
2155
 
1915
2156
 error:
1997
2238
        int nfd;
1998
2239
 
1999
2240
        if ((nfd = accept(fd, (struct sockaddr *)&ss, &addrlen)) == -1) {
2000
 
                event_warn("%s: bad accept", __func__);
 
2241
                if (errno != EAGAIN && errno != EINTR)
 
2242
                        event_warn("%s: bad accept", __func__);
2001
2243
                return;
2002
2244
        }
2003
2245
        if (evutil_make_socket_nonblocking(nfd) < 0)
2012
2254
        int fd;
2013
2255
        int res;
2014
2256
 
2015
 
        if ((fd = bind_socket(address, port)) == -1)
 
2257
        if ((fd = bind_socket(address, port, 1 /*reuse*/)) == -1)
2016
2258
                return (-1);
2017
2259
 
2018
2260
        if (listen(fd, 128) == -1) {
2277
2519
const char *
2278
2520
evhttp_request_uri(struct evhttp_request *req) {
2279
2521
        if (req->uri == NULL)
2280
 
                event_debug(("%s: request %p has no uri\n", req));
 
2522
                event_debug(("%s: request %p has no uri\n", __func__, req));
2281
2523
        return (req->uri);
2282
2524
}
2283
2525
 
2292
2534
        int fd, struct sockaddr *sa, socklen_t salen)
2293
2535
{
2294
2536
        struct evhttp_connection *evcon;
2295
 
        char *hostname, *portname;
 
2537
        char *hostname = NULL, *portname = NULL;
2296
2538
 
2297
2539
        name_from_addr(sa, salen, &hostname, &portname);
 
2540
        if (hostname == NULL || portname == NULL) {
 
2541
                if (hostname) free(hostname);
 
2542
                if (portname) free(portname);
 
2543
                return (NULL);
 
2544
        }
 
2545
 
2298
2546
        event_debug(("%s: new request from %s:%s on %d\n",
2299
2547
                        __func__, hostname, portname, fd));
2300
2548
 
2301
2549
        /* we need a connection object to put the http request on */
2302
 
        if ((evcon = evhttp_connection_new(hostname, atoi(portname))) == NULL)
 
2550
        evcon = evhttp_connection_new(hostname, atoi(portname));
 
2551
        free(hostname);
 
2552
        free(portname);
 
2553
        if (evcon == NULL)
2303
2554
                return (NULL);
2304
2555
 
2305
2556
        /* associate the base if we have one*/
2306
2557
        evhttp_connection_set_base(evcon, http->base);
2307
2558
 
2308
2559
        evcon->flags |= EVHTTP_CON_INCOMING;
2309
 
        evcon->state = EVCON_CONNECTED;
 
2560
        evcon->state = EVCON_READING_FIRSTLINE;
2310
2561
        
2311
2562
        evcon->fd = fd;
2312
2563
 
2398
2649
name_from_addr(struct sockaddr *sa, socklen_t salen,
2399
2650
    char **phost, char **pport)
2400
2651
{
 
2652
        char ntop[NI_MAXHOST];
 
2653
        char strport[NI_MAXSERV];
 
2654
        int ni_result;
 
2655
 
2401
2656
#ifdef HAVE_GETNAMEINFO
2402
 
        /* XXXX not threadsafe. */
2403
 
        static char ntop[NI_MAXHOST];
2404
 
        static char strport[NI_MAXSERV];
2405
 
        int ni_result;
2406
 
 
2407
 
        if ((ni_result = getnameinfo(sa, salen,
 
2657
        ni_result = getnameinfo(sa, salen,
2408
2658
                ntop, sizeof(ntop), strport, sizeof(strport),
2409
 
                NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
 
2659
                NI_NUMERICHOST|NI_NUMERICSERV);
 
2660
        
 
2661
        if (ni_result != 0) {
2410
2662
                if (ni_result == EAI_SYSTEM)
2411
2663
                        event_err(1, "getnameinfo failed");
2412
2664
                else
2413
2665
                        event_errx(1, "getnameinfo failed: %s", gai_strerror(ni_result));
 
2666
                return;
2414
2667
        }
2415
 
 
2416
 
        *phost = ntop;
2417
 
        *pport = strport;
2418
2668
#else
2419
 
        /* XXXX */
 
2669
        ni_result = fake_getnameinfo(sa, salen,
 
2670
                ntop, sizeof(ntop), strport, sizeof(strport),
 
2671
                NI_NUMERICHOST|NI_NUMERICSERV);
 
2672
        if (ni_result != 0)
 
2673
                        return;
2420
2674
#endif
 
2675
        *phost = strdup(ntop);
 
2676
        *pport = strdup(strport);
2421
2677
}
2422
2678
 
2423
 
/* Either connect or bind */
2424
 
 
 
2679
/* Create a non-blocking socket and bind it */
 
2680
/* todo: rename this function */
2425
2681
static int
2426
 
bind_socket_ai(struct addrinfo *ai)
 
2682
bind_socket_ai(struct addrinfo *ai, int reuse)
2427
2683
{
2428
2684
        int fd, on = 1, r;
2429
2685
        int serrno;
2446
2702
#endif
2447
2703
 
2448
2704
        setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
2449
 
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
 
2705
        if (reuse) {
 
2706
                setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
 
2707
                    (void *)&on, sizeof(on));
 
2708
        }
2450
2709
 
2451
 
        r = bind(fd, ai->ai_addr, ai->ai_addrlen);
2452
 
        if (r == -1)
2453
 
                goto out;
 
2710
        if (ai != NULL) {
 
2711
                r = bind(fd, ai->ai_addr, ai->ai_addrlen);
 
2712
                if (r == -1)
 
2713
                        goto out;
 
2714
        }
2454
2715
 
2455
2716
        return (fd);
2456
2717
 
2475
2736
        ai.ai_family = AF_INET;
2476
2737
        ai.ai_socktype = SOCK_STREAM;
2477
2738
        ai.ai_flags = AI_PASSIVE;  /* turn NULL host name into INADDR_ANY */
2478
 
        snprintf(strport, sizeof(strport), "%d", port);
 
2739
        evutil_snprintf(strport, sizeof(strport), "%d", port);
2479
2740
        if ((ai_result = getaddrinfo(address, strport, &ai, &aitop)) != 0) {
2480
2741
                if ( ai_result == EAI_SYSTEM )
2481
2742
                        event_warn("getaddrinfo");
2500
2761
}
2501
2762
 
2502
2763
static int
2503
 
bind_socket(const char *address, u_short port)
 
2764
bind_socket(const char *address, u_short port, int reuse)
2504
2765
{
2505
2766
        int fd;
2506
 
        struct addrinfo *aitop = make_addrinfo(address, port);
 
2767
        struct addrinfo *aitop = NULL;
 
2768
 
 
2769
        /* just create an unbound socket */
 
2770
        if (address == NULL && port == 0)
 
2771
                return bind_socket_ai(NULL, 0);
 
2772
                
 
2773
        aitop = make_addrinfo(address, port);
2507
2774
 
2508
2775
        if (aitop == NULL)
2509
2776
                return (-1);
2510
2777
 
2511
 
        fd = bind_socket_ai(aitop);
 
2778
        fd = bind_socket_ai(aitop, reuse);
2512
2779
 
2513
2780
#ifdef HAVE_GETADDRINFO
2514
2781
        freeaddrinfo(aitop);