| 6 |
| * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 |
| 28 | #include "config.h" |
| 29 |
| 30 | #ifdef HAVE_SYS_PARAM_H |
| 31 | #include <sys/param.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_SYS_TYPES_H |
| 34 | #include <sys/types.h> |
| 35 | #endif |
| 36 |
| 37 | #ifdef HAVE_SYS_TIME_H |
| 38 | #include <sys/time.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_SYS_IOCCOM_H |
| 41 | #include <sys/ioccom.h> |
| 42 | #endif |
| 43 |
| 44 | #ifndef WIN32 |
| 45 | #include <sys/resource.h> |
| 46 | #include <sys/socket.h> |
| 47 | #include <sys/stat.h> |
| 48 | #include <sys/wait.h> |
| 49 | #endif |
| 50 |
| 51 | #include <sys/queue.h> |
| 52 |
| 53 | #ifndef WIN32 |
| 54 | #include <netinet/in.h> |
| 55 | #include <netdb.h> |
| 56 | #endif |
| 57 |
| 58 | #ifdef WIN32 |
| 59 | #include <winsock2.h> |
| 60 | #endif |
| 61 |
| 62 | #include <assert.h> |
| 63 | #include <ctype.h> |
| 64 | #include <errno.h> |
| 65 | #include <stdio.h> |
| 66 | #include <stdlib.h> |
| 67 | #include <string.h> |
| 68 | #ifndef WIN32 |
| 69 | #include <syslog.h> |
| 70 | #endif |
| 71 | #include <signal.h> |
| 72 | #include <time.h> |
| 73 | #ifdef HAVE_UNISTD_H |
| 74 | #include <unistd.h> |
| 75 | #endif |
| 76 | #ifdef HAVE_FCNTL_H |
| 77 | #include <fcntl.h> |
| 78 | #endif |
| 79 |
| 80 | #undef timeout_pending |
| 81 | #undef timeout_initialized |
| 82 |
| 83 | #include "strlcpy-internal.h" |
| 84 | #include "event.h" |
| 85 | #include "evhttp.h" |
| 86 | #include "evutil.h" |
| 87 | #include "log.h" |
| 88 | #include "http-internal.h" |
| 89 |
| 90 | #ifdef WIN32 |
| 91 | #define strcasecmp _stricmp |
| 92 | #define strncasecmp _strnicmp |
| 93 | #define strdup _strdup |
| 94 | #endif |
| 95 |
| 96 | #ifndef HAVE_GETNAMEINFO |
| 97 | #define NI_MAXSERV 32 |
| 98 | #define NI_MAXHOST 1025 |
| 99 |
| 100 | #ifndef NI_NUMERICHOST |
| 101 | #define NI_NUMERICHOST 1 |
| 102 | #endif |
| 103 |
| 104 | #ifndef NI_NUMERICSERV |
| 105 | #define NI_NUMERICSERV 2 |
| 106 | #endif |
| 107 |
| 108 | static int |
| 109 | fake_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, |
| 110 | size_t hostlen, char *serv, size_t servlen, int flags) |
| 111 | { |
| 112 | struct sockaddr_in *sin = (struct sockaddr_in *)sa; |
| 113 |
| 114 | if (serv != NULL) { |
| 115 | char tmpserv[16]; |
| 116 | evutil_snprintf(tmpserv, sizeof(tmpserv), |
| 117 | "%d", ntohs(sin->sin_port)); |
| 118 | if (strlcpy(serv, tmpserv, servlen) >= servlen) |
| 119 | return (-1); |
| 120 | } |
| 121 |
| 122 | if (host != NULL) { |
| 123 | if (flags & NI_NUMERICHOST) { |
| 124 | if (strlcpy(host, inet_ntoa(sin->sin_addr), |
| 125 | hostlen) >= hostlen) |
| 126 | return (-1); |
| 127 | else |
| 128 | return (0); |
| 129 | } else { |
| 130 | struct hostent *hp; |
| 131 | hp = gethostbyaddr((char *)&sin->sin_addr, |
| 132 | sizeof(struct in_addr), AF_INET); |
| 133 | if (hp == NULL) |
| 134 | return (-2); |
| 135 |
| 136 | if (strlcpy(host, hp->h_name, hostlen) >= hostlen) |
| 137 | return (-1); |
| 138 | else |
| 139 | return (0); |
| 140 | } |
| 141 | } |
| 142 | return (0); |
| 143 | } |
| 144 |
| 145 | #endif |
| 146 |
| 147 | #ifndef HAVE_GETADDRINFO |
| 148 | /* Apparently msvc2010 does have an addrinfo definition visible here */ |
| 149 | #if !defined(WIN32) || !defined(_MSC_VER) || (_MSC_VER < 1600) |
| 150 | struct addrinfo { |
| 151 | int ai_family; |
| 152 | int ai_socktype; |
| 153 | int ai_protocol; |
| 154 | size_t ai_addrlen; |
| 155 | struct sockaddr *ai_addr; |
| 156 | struct addrinfo *ai_next; |
| 157 | }; |
| 158 | #endif |
| 159 | static int |
| 160 | fake_getaddrinfo(const char *hostname, struct addrinfo *ai) |
| 161 | { |
| 162 | struct hostent *he = NULL; |
| 163 | struct sockaddr_in *sa; |
| 164 | if (hostname) { |
| 165 | he = gethostbyname(hostname); |
| 166 | if (!he) |
| 167 | return (-1); |
| 168 | } |
| 169 | ai->ai_family = he ? he->h_addrtype : AF_INET; |
| 170 | ai->ai_socktype = SOCK_STREAM; |
| 171 | ai->ai_protocol = 0; |
| 172 | ai->ai_addrlen = sizeof(struct sockaddr_in); |
| 173 | if (NULL == (ai->ai_addr = malloc(ai->ai_addrlen))) |
| 174 | return (-1); |
| 175 | sa = (struct sockaddr_in*)ai->ai_addr; |
| 176 | memset(sa, 0, ai->ai_addrlen); |
| 177 | if (he) { |
| 178 | sa->sin_family = he->h_addrtype; |
| 179 | memcpy(&sa->sin_addr, he->h_addr_list[0], he->h_length); |
| 180 | } else { |
| 181 | sa->sin_family = AF_INET; |
| 182 | sa->sin_addr.s_addr = INADDR_ANY; |
| 183 | } |
| 184 | ai->ai_next = NULL; |
| 185 | return (0); |
| 186 | } |
| 187 | static void |
| 188 | fake_freeaddrinfo(struct addrinfo *ai) |
| 189 | { |
| 190 | free(ai->ai_addr); |
| 191 | } |
| 192 | #endif |
| 193 |
| 194 | #ifndef MIN |
| 195 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
| 196 | #endif |
| 197 |
| 198 | /* wrapper for setting the base from the http server */ |
| 199 | #define EVHTTP_BASE_SET(x, y) do { \ |
| 200 | if ((x)->base != NULL) event_base_set((x)->base, y); \ |
| 201 | } while (0) |
| 202 |
| 203 | extern int debug; |
| 204 |
| 205 | static int socket_connect(int fd, const char *address, unsigned short port); |
| 206 | static int bind_socket_ai(struct addrinfo *, int reuse); |
| 207 | static int bind_socket(const char *, u_short, int reuse); |
| 208 | static void name_from_addr(struct sockaddr *, socklen_t, char **, char **); |
| 209 | static int evhttp_associate_new_request_with_connection( |
| 210 | struct evhttp_connection *evcon); |
| 211 | static void evhttp_connection_start_detectclose( |
| 212 | struct evhttp_connection *evcon); |
| 213 | static void evhttp_connection_stop_detectclose( |
| 214 | struct evhttp_connection *evcon); |
| 215 | static void evhttp_request_dispatch(struct evhttp_connection* evcon); |
| 216 | static void evhttp_read_firstline(struct evhttp_connection *evcon, |
| 217 | struct evhttp_request *req); |
| 218 | static void evhttp_read_header(struct evhttp_connection *evcon, |
| 219 | struct evhttp_request *req); |
| 220 | static int evhttp_add_header_internal(struct evkeyvalq *headers, |
| 221 | const char *key, const char *value); |
| 222 | static int evhttp_decode_uri_internal(const char *uri, size_t length, |
| 223 | char *ret, int always_decode_plus); |
| 224 |
| 225 | void evhttp_read(int, short, void *); |
| 226 | void evhttp_write(int, short, void *); |
| 227 |
| 228 | #ifndef HAVE_STRSEP |
| 229 | /* strsep replacement for platforms that lack it. Only works if |
| 230 | * del is one character long. */ |
| 231 | static char * |
| 232 | strsep(char **s, const char *del) |
| 233 | { |
| 234 | char *d, *tok; |
| 235 | assert(strlen(del) == 1); |
| 236 | if (!s || !*s) |
| 237 | return NULL; |
| 238 | tok = *s; |
| 239 | d = strstr(tok, del); |
| 240 | if (d) { |
| 241 | *d = '\0'; |
| 242 | *s = d + 1; |
| 243 | } else |
| 244 | *s = NULL; |
| 245 | return tok; |
| 246 | } |
| 247 | #endif |
| 248 |
| 249 | static const char * |
| 250 | html_replace(char ch, char *buf) |
| 251 | { |
| 252 | switch (ch) { |
| 253 | case '<': |
| 254 | return "<"; |
| 255 | case '>': |
| 256 | return ">"; |
| 257 | case '"': |
| 258 | return """; |
| 259 | case '\'': |
| 260 | return "'"; |
| 261 | case '&': |
| 262 | return "&"; |
| 263 | default: |
| 264 | break; |
| 265 | } |
| 266 |
| 267 | /* Echo the character back */ |
| 268 | buf[0] = ch; |
| 269 | buf[1] = '\0'; |
| 270 |
| 271 | return buf; |
| 272 | } |
| 273 |
| 274 | /* |
| 275 | * Replaces <, >, ", ' and & with <, >, ", |
| 276 | * ' and & correspondingly. |
| 277 | * |
| 278 | * The returned string needs to be freed by the caller. |
| 279 | */ |
| 280 |
| 281 | char * |
| 282 | evhttp_htmlescape(const char *html) |
| 283 | { |
| 284 | int i, new_size = 0, old_size = strlen(html); |
| 285 | char *escaped_html, *p; |
| 286 | char scratch_space[2]; |
| 287 |
| 288 | for (i = 0; i < old_size; ++i) |
| 289 | new_size += strlen(html_replace(html[i], scratch_space)); |
| 290 |
| 291 | p = escaped_html = malloc(new_size + 1); |
| 292 | if (escaped_html == NULL) |
| 293 | event_err(1, "%s: malloc(%d)", __func__, new_size + 1); |
| 294 | for (i = 0; i < old_size; ++i) { |
| 295 | const char *replaced = html_replace(html[i], scratch_space); |
| 296 | /* this is length checked */ |
| 297 | strcpy(p, replaced); |
| 298 | p += strlen(replaced); |
| 299 | } |
| 300 |
| 301 | *p = '\0'; |
| 302 |
| 303 | return (escaped_html); |
| 304 | } |
| 305 |
| 306 | static const char * |
| 307 | evhttp_method(enum evhttp_cmd_type type) |
| 308 | { |
| 309 | const char *method; |
| 310 |
| 311 | switch (type) { |
| 312 | case EVHTTP_REQ_GET: |
| 313 | method = "GET"; |
| 314 | break; |
| 315 | case EVHTTP_REQ_POST: |
| 316 | method = "POST"; |
| 317 | break; |
| 318 | case EVHTTP_REQ_HEAD: |
| 319 | method = "HEAD"; |
| 320 | break; |
| 321 | default: |
| 322 | method = NULL; |
| 323 | break; |
| 324 | } |
| 325 |
| 326 | return (method); |
| 327 | } |
| 328 |
| 329 | static void |
| 330 | evhttp_add_event(struct event *ev, int timeout, int default_timeout) |
| 331 | { |
| 332 | if (timeout != 0) { |
| 333 | struct timeval tv; |
| 334 |
| 335 | evutil_timerclear(&tv); |
| 336 | tv.tv_sec = timeout != -1 ? timeout : default_timeout; |
| 337 | event_add(ev, &tv); |
| 338 | } else { |
| 339 | event_add(ev, NULL); |
| 340 | } |
| 341 | } |
| 342 |
| 343 | void |
| 344 | evhttp_write_buffer(struct evhttp_connection *evcon, |
| 345 | void (*cb)(struct evhttp_connection *, void *), void *arg) |
| 346 | { |
| 347 | event_debug(("%s: preparing to write buffer\n", __func__)); |
| 348 |
| 349 | /* Set call back */ |
| 350 | evcon->cb = cb; |
| 351 | evcon->cb_arg = arg; |
| 352 |
| 353 | /* check if the event is already pending */ |
| 354 | if (event_pending(&evcon->ev, EV_WRITE|EV_TIMEOUT, NULL)) |
| 355 | event_del(&evcon->ev); |
| 356 |
| 357 | event_set(&evcon->ev, evcon->fd, EV_WRITE, evhttp_write, evcon); |
| 358 | EVHTTP_BASE_SET(evcon, &evcon->ev); |
| 359 | evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_WRITE_TIMEOUT); |
| 360 | } |
| 361 |
| 362 | static int |
| 363 | evhttp_connected(struct evhttp_connection *evcon) |
| 364 | { |
| 365 | switch (evcon->state) { |
| 366 | case EVCON_DISCONNECTED: |
| 367 | case EVCON_CONNECTING: |
| 368 | return (0); |
| 369 | case EVCON_IDLE: |
| 370 | case EVCON_READING_FIRSTLINE: |
| 371 | case EVCON_READING_HEADERS: |
| 372 | case EVCON_READING_BODY: |
| 373 | case EVCON_READING_TRAILER: |
| 374 | case EVCON_WRITING: |
| 375 | default: |
| 376 | return (1); |
| 377 | } |
| 378 | } |
| 379 |
| 380 | /* |
| 381 | * Create the headers needed for an HTTP request |
| 382 | */ |
| 383 | static void |
| 384 | evhttp_make_header_request(struct evhttp_connection *evcon, |
| 385 | struct evhttp_request *req) |
| 386 | { |
| 387 | const char *method; |
| 388 |
| 389 | evhttp_remove_header(req->output_headers, "Proxy-Connection"); |
| 390 |
| 391 | /* Generate request line */ |
| 392 | method = evhttp_method(req->type); |
| 393 | evbuffer_add_printf(evcon->output_buffer, "%s %s HTTP/%d.%d\r\n", |
| 394 | method, req->uri, req->major, req->minor); |
| 395 |
| 396 | /* Add the content length on a post request if missing */ |
| 397 | if (req->type == EVHTTP_REQ_POST && |
| 398 | evhttp_find_header(req->output_headers, "Content-Length") == NULL){ |
| 399 | char size[22]; |
| 400 | evutil_snprintf(size, sizeof(size), "%ld", |
| 401 | (long)EVBUFFER_LENGTH(req->output_buffer)); |
| 402 | evhttp_add_header(req->output_headers, "Content-Length", size); |
| 403 | } |
| 404 | } |
| 405 |
| 406 | static int |
| 407 | evhttp_is_connection_close(int flags, struct evkeyvalq* headers) |
| 408 | { |
| 409 | if (flags & EVHTTP_PROXY_REQUEST) { |
| 410 | /* proxy connection */ |
| 411 | const char *connection = evhttp_find_header(headers, "Proxy-Connection"); |
| 412 | return (connection == NULL || strcasecmp(connection, "keep-alive") != 0); |
| 413 | } else { |
| 414 | const char *connection = evhttp_find_header(headers, "Connection"); |
| 415 | return (connection != NULL && strcasecmp(connection, "close") == 0); |
| 416 | } |
| 417 | } |
| 418 |
| 419 | static int |
| 420 | evhttp_is_connection_keepalive(struct evkeyvalq* headers) |
| 421 | { |
| 422 | const char *connection = evhttp_find_header(headers, "Connection"); |
| 423 | return (connection != NULL |
| 424 | && strncasecmp(connection, "keep-alive", 10) == 0); |
| 425 | } |
| 426 |
| 427 | static void |
| 428 | evhttp_maybe_add_date_header(struct evkeyvalq *headers) |
| 429 | { |
| 430 | if (evhttp_find_header(headers, "Date") == NULL) { |
| 431 | char date[50]; |
| 432 | #ifndef WIN32 |
| 433 | struct tm cur; |
| 434 | #endif |
| 435 | struct tm *cur_p; |
| 436 | time_t t = time(NULL); |
| 437 | #ifdef WIN32 |
| 438 | cur_p = gmtime(&t); |
| 439 | #else |
| 440 | gmtime_r(&t, &cur); |
| 441 | cur_p = &cur; |
| 442 | #endif |
| 443 | if (strftime(date, sizeof(date), |
| 444 | "%a, %d %b %Y %H:%M:%S GMT", cur_p) != 0) { |
| 445 | evhttp_add_header(headers, "Date", date); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 |
| 450 | static void |
| 451 | evhttp_maybe_add_content_length_header(struct evkeyvalq *headers, |
| 452 | long content_length) |
| 453 | { |
| 454 | if (evhttp_find_header(headers, "Transfer-Encoding") == NULL && |
| 455 | evhttp_find_header(headers, "Content-Length") == NULL) { |
| 456 | char len[22]; |
| 457 | evutil_snprintf(len, sizeof(len), "%ld", content_length); |
| 458 | evhttp_add_header(headers, "Content-Length", len); |
| 459 | } |
| 460 | } |
| 461 |
| 462 | /* |
| 463 | * Create the headers needed for an HTTP reply |
| 464 | */ |
| 465 |
| 466 | static void |
| 467 | evhttp_make_header_response(struct evhttp_connection *evcon, |
| 468 | struct evhttp_request *req) |
| 469 | { |
| 470 | int is_keepalive = evhttp_is_connection_keepalive(req->input_headers); |
| 471 | evbuffer_add_printf(evcon->output_buffer, "HTTP/%d.%d %d %s\r\n", |
| 472 | req->major, req->minor, req->response_code, |
| 473 | req->response_code_line); |
| 474 |
| 475 | if (req->major == 1) { |
| 476 | if (req->minor == 1) |
| 477 | evhttp_maybe_add_date_header(req->output_headers); |
| 478 |
| 479 | /* |
| 480 | * if the protocol is 1.0; and the connection was keep-alive |
| 481 | * we need to add a keep-alive header, too. |
| 482 | */ |
| 483 | if (req->minor == 0 && is_keepalive) |
| 484 | evhttp_add_header(req->output_headers, |
| 485 | "Connection", "keep-alive"); |
| 486 |
| 487 | if (req->minor == 1 || is_keepalive) { |
| 488 | /* |
| 489 | * we need to add the content length if the |
| 490 | * user did not give it, this is required for |
| 491 | * persistent connections to work. |
| 492 | */ |
| 493 | evhttp_maybe_add_content_length_header( |
| 494 | req->output_headers, |
| 495 | (long)EVBUFFER_LENGTH(req->output_buffer)); |
| 496 | } |
| 497 | } |
| 498 |
| 499 | /* Potentially add headers for unidentified content. */ |
| 500 | if (EVBUFFER_LENGTH(req->output_buffer)) { |
| 501 | if (evhttp_find_header(req->output_headers, |
| 502 | "Content-Type") == NULL) { |
| 503 | evhttp_add_header(req->output_headers, |
| 504 | "Content-Type", "text/html; charset=ISO-8859-1"); |
| 505 | } |
| 506 | } |
| 507 |
| 508 | /* if the request asked for a close, we send a close, too */ |
| 509 | if (evhttp_is_connection_close(req->flags, req->input_headers)) { |
| 510 | evhttp_remove_header(req->output_headers, "Connection"); |
| 511 | if (!(req->flags & EVHTTP_PROXY_REQUEST)) |
| 512 | evhttp_add_header(req->output_headers, "Connection", "close"); |
| 513 | evhttp_remove_header(req->output_headers, "Proxy-Connection"); |
| 514 | } |
| 515 | } |
| 516 |
| 517 | void |
| 518 | evhttp_make_header(struct evhttp_connection *evcon, struct evhttp_request *req) |
| 519 | { |
| 520 | struct evkeyval *header; |
| 521 |
| 522 | /* |
| 523 | * Depending if this is a HTTP request or response, we might need to |
| 524 | * add some new headers or remove existing headers. |
| 525 | */ |
| 526 | if (req->kind == EVHTTP_REQUEST) { |
| 527 | evhttp_make_header_request(evcon, req); |
| 528 | } else { |
| 529 | evhttp_make_header_response(evcon, req); |
| 530 | } |
| 531 |
| 532 | TAILQ_FOREACH(header, req->output_headers, next) { |
| 533 | evbuffer_add_printf(evcon->output_buffer, "%s: %s\r\n", |
| 534 | header->key, header->value); |
| 535 | } |
| 536 | evbuffer_add(evcon->output_buffer, "\r\n", 2); |
| 537 |
| 538 | if (EVBUFFER_LENGTH(req->output_buffer) > 0) { |
| 539 | /* |
| 540 | * For a request, we add the POST data, for a reply, this |
| 541 | * is the regular data. |
| 542 | */ |
| 543 | evbuffer_add_buffer(evcon->output_buffer, req->output_buffer); |
| 544 | } |
| 545 | } |
| 546 |
| 547 | /* Separated host, port and file from URI */ |
| 548 |
| 549 | int |
| 550 | evhttp_hostportfile(char *url, char **phost, u_short *pport, char **pfile) |
| 551 | { |
| 552 | /* XXX not threadsafe. */ |
| 553 | static char host[1024]; |
| 554 | static char file[1024]; |
| 555 | char *p; |
| 556 | const char *p2; |
| 557 | int len; |
| 558 | u_short port; |
| 559 |
| 560 | len = strlen(HTTP_PREFIX); |
| 561 | if (strncasecmp(url, HTTP_PREFIX, len)) |
| 562 | return (-1); |
| 563 |
| 564 | url += len; |
| 565 |
| 566 | /* We might overrun */ |
| 567 | if (strlcpy(host, url, sizeof (host)) >= sizeof(host)) |
| 568 | return (-1); |
| 569 |
| 570 | p = strchr(host, '/'); |
| 571 | if (p != NULL) { |
| 572 | *p = '\0'; |
| 573 | p2 = p + 1; |
| 574 | } else |
| 575 | p2 = NULL; |
| 576 |
| 577 | if (pfile != NULL) { |
| 578 | /* Generate request file */ |
| 579 | if (p2 == NULL) |
| 580 | p2 = ""; |
| 581 | evutil_snprintf(file, sizeof(file), "/%s", p2); |
| 582 | } |
| 583 |
| 584 | p = strchr(host, ':'); |
| 585 | if (p != NULL) { |
| 586 | *p = '\0'; |
| 587 | port = atoi(p + 1); |
| 588 |
| 589 | if (port == 0) |
| 590 | return (-1); |
| 591 | } else |
| 592 | port = HTTP_DEFAULTPORT; |
| 593 |
| 594 | if (phost != NULL) |
| 595 | *phost = host; |
| 596 | if (pport != NULL) |
| 597 | *pport = port; |
| 598 | if (pfile != NULL) |
| 599 | *pfile = file; |
| 600 |
| 601 | return (0); |
| 602 | } |
| 603 |
| 604 | static int |
| 605 | evhttp_connection_incoming_fail(struct evhttp_request *req, |
| 606 | enum evhttp_connection_error error) |
| 607 | { |
| 608 | switch (error) { |
| 609 | case EVCON_HTTP_TIMEOUT: |
| 610 | case EVCON_HTTP_EOF: |
| 611 | /* |
| 612 | * these are cases in which we probably should just |
| 613 | * close the connection and not send a reply. this |
| 614 | * case may happen when a browser keeps a persistent |
| 615 | * connection open and we timeout on the read. when |
| 616 | * the request is still being used for sending, we |
| 617 | * need to disassociated it from the connection here. |
| 618 | */ |
| 619 | if (!req->userdone) { |
| 620 | /* remove it so that it will not be freed */ |
| 621 | TAILQ_REMOVE(&req->evcon->requests, req, next); |
| 622 | /* indicate that this request no longer has a |
| 623 | * connection object |
| 624 | */ |
| 625 | req->evcon = NULL; |
| 626 | } |
| 627 | return (-1); |
| 628 | case EVCON_HTTP_INVALID_HEADER: |
| 629 | default: /* xxx: probably should just error on default */ |
| 630 | /* the callback looks at the uri to determine errors */ |
| 631 | if (req->uri) { |
| 632 | free(req->uri); |
| 633 | req->uri = NULL; |
| 634 | } |
| 635 |
| 636 | /* |
| 637 | * the callback needs to send a reply, once the reply has |
| 638 | * been send, the connection should get freed. |
| 639 | */ |
| 640 | (*req->cb)(req, req->cb_arg); |
| 641 | } |
| 642 |
| 643 | return (0); |
| 644 | } |
| 645 |
| 646 | void |
| 647 | evhttp_connection_fail(struct evhttp_connection *evcon, |
| 648 | enum evhttp_connection_error error) |
| 649 | { |
| 650 | struct evhttp_request* req = TAILQ_FIRST(&evcon->requests); |
| 651 | void (*cb)(struct evhttp_request *, void *); |
| 652 | void *cb_arg; |
| 653 | assert(req != NULL); |
| 654 |
| 655 | if (evcon->flags & EVHTTP_CON_INCOMING) { |
| 656 | /* |
| 657 | * for incoming requests, there are two different |
| 658 | * failure cases. it's either a network level error |
| 659 | * or an http layer error. for problems on the network |
| 660 | * layer like timeouts we just drop the connections. |
| 661 | * For HTTP problems, we might have to send back a |
| 662 | * reply before the connection can be freed. |
| 663 | */ |
| 664 | if (evhttp_connection_incoming_fail(req, error) == -1) |
| 665 | evhttp_connection_free(evcon); |
| 666 | return; |
| 667 | } |
| 668 |
| 669 | /* save the callback for later; the cb might free our object */ |
| 670 | cb = req->cb; |
| 671 | cb_arg = req->cb_arg; |
| 672 |
| 673 | /* do not fail all requests; the next request is going to get |
| 674 | * send over a new connection. when a user cancels a request, |
| 675 | * all other pending requests should be processed as normal |
| 676 | */ |
| 677 | TAILQ_REMOVE(&evcon->requests, req, next); |
| 678 | evhttp_request_free(req); |
| 679 |
| 680 | /* reset the connection */ |
| 681 | evhttp_connection_reset(evcon); |
| 682 |
| 683 | /* We are trying the next request that was queued on us */ |
| 684 | if (TAILQ_FIRST(&evcon->requests) != NULL) |
| 685 | evhttp_connection_connect(evcon); |
| 686 |
| 687 | /* inform the user */ |
| 688 | if (cb != NULL) |
| 689 | (*cb)(NULL, cb_arg); |
| 690 | } |
| 691 |
| 692 | void |
| 693 | evhttp_write(int fd, short what, void *arg) |
| 694 | { |
| 695 | struct evhttp_connection *evcon = arg; |
| 696 | int n; |
| 697 |
| 698 | if (what == EV_TIMEOUT) { |
| 699 | evhttp_connection_fail(evcon, EVCON_HTTP_TIMEOUT); |
| 700 | return; |
| 701 | } |
| 702 |
| 703 | n = evbuffer_write(evcon->output_buffer, fd); |
| 704 | if (n == -1) { |
| 705 | event_debug(("%s: evbuffer_write", __func__)); |
| 706 | evhttp_connection_fail(evcon, EVCON_HTTP_EOF); |
| 707 | return; |
| 708 | } |
| 709 |
| 710 | if (n == 0) { |
| 711 | event_debug(("%s: write nothing", __func__)); |
| 712 | evhttp_connection_fail(evcon, EVCON_HTTP_EOF); |
| 713 | return; |
| 714 | } |
| 715 |
| 716 | if (EVBUFFER_LENGTH(evcon->output_buffer) != 0) { |
| 717 | evhttp_add_event(&evcon->ev, |
| 718 | evcon->timeout, HTTP_WRITE_TIMEOUT); |
| 719 | return; |
| 720 | } |
| 721 |
| 722 | /* Activate our call back */ |
| 723 | if (evcon->cb != NULL) |
| 724 | (*evcon->cb)(evcon, evcon->cb_arg); |
| 725 | } |
| 726 |
| 727 | /** |
| 728 | * Advance the connection state. |
| 729 | * - If this is an outgoing connection, we've just processed the response; |
| 730 | * idle or close the connection. |
| 731 | * - If this is an incoming connection, we've just processed the request; |
| 732 | * respond. |
| 733 | */ |
| 734 | static void |
| 735 | evhttp_connection_done(struct evhttp_connection *evcon) |
| 736 | { |
| 737 | struct evhttp_request *req = TAILQ_FIRST(&evcon->requests); |
| 738 | int con_outgoing = evcon->flags & EVHTTP_CON_OUTGOING; |
| 739 |
| 740 | if (con_outgoing) { |
| 741 | /* idle or close the connection */ |
| 742 | int need_close; |
| 743 | TAILQ_REMOVE(&evcon->requests, req, next); |
| 744 | req->evcon = NULL; |
| 745 |
| 746 | evcon->state = EVCON_IDLE; |
| 747 |
| 748 | need_close = |
| 749 | evhttp_is_connection_close(req->flags, req->input_headers)|| |
| 750 | evhttp_is_connection_close(req->flags, req->output_headers); |
| 751 |
| 752 | /* check if we got asked to close the connection */ |
| 753 | if (need_close) |
| 754 | evhttp_connection_reset(evcon); |
| 755 |
| 756 | if (TAILQ_FIRST(&evcon->requests) != NULL) { |
| 757 | /* |
| 758 | * We have more requests; reset the connection |
| 759 | * and deal with the next request. |
| 760 | */ |
| 761 | if (!evhttp_connected(evcon)) |
| 762 | evhttp_connection_connect(evcon); |
| 763 | else |
| 764 | evhttp_request_dispatch(evcon); |
| 765 | } else if (!need_close) { |
| 766 | /* |
| 767 | * The connection is going to be persistent, but we |
| 768 | * need to detect if the other side closes it. |
| 769 | */ |
| 770 | evhttp_connection_start_detectclose(evcon); |
| 771 | } |
| 772 | } else if (evcon->state != EVCON_DISCONNECTED) { |
| 773 | /* |
| 774 | * incoming connection - we need to leave the request on the |
| 775 | * connection so that we can reply to it. |
| 776 | */ |
| 777 | evcon->state = EVCON_WRITING; |
| 778 | } |
| 779 |
| 780 | /* notify the user of the request */ |
| 781 | (*req->cb)(req, req->cb_arg); |
| 782 |
| 783 | /* if this was an outgoing request, we own and it's done. so free it */ |
| 784 | if (con_outgoing) { |
| 785 | evhttp_request_free(req); |
| 786 | } |
| 787 | } |
| 788 |
| 789 | /* |
| 790 | * Handles reading from a chunked request. |
| 791 | * return ALL_DATA_READ: |
| 792 | * all data has been read |
| 793 | * return MORE_DATA_EXPECTED: |
| 794 | * more data is expected |
| 795 | * return DATA_CORRUPTED: |
| 796 | * data is corrupted |
| 797 | * return REQUEST_CANCLED: |
| 798 | * request was canceled by the user calling evhttp_cancel_request |
| 799 | */ |
| 800 |
| 801 | static enum message_read_status |
| 802 | evhttp_handle_chunked_read(struct evhttp_request *req, struct evbuffer *buf) |
| 803 | { |
| 804 | int len; |
| 805 |
| 806 | while ((len = EVBUFFER_LENGTH(buf)) > 0) { |
| 807 | if (req->ntoread < 0) { |
| 808 | /* Read chunk size */ |
| 809 | ev_int64_t ntoread; |
| 810 | char *p = evbuffer_readline(buf); |
| 811 | char *endp; |
| 812 | int error; |
| 813 | if (p == NULL) |
| 814 | break; |
| 815 | /* the last chunk is on a new line? */ |
| 816 | if (strlen(p) == 0) { |
| 817 | free(p); |
| 818 | continue; |
| 819 | } |
| 820 | ntoread = evutil_strtoll(p, &endp, 16); |
| 821 | error = (*p == '\0' || |
| 822 | (*endp != '\0' && *endp != ' ') || |
| 823 | ntoread < 0); |
| 824 | free(p); |
| 825 | if (error) { |
| 826 | /* could not get chunk size */ |
| 827 | return (DATA_CORRUPTED); |
| 828 | } |
| 829 | req->ntoread = ntoread; |
| 830 | if (req->ntoread == 0) { |
| 831 | /* Last chunk */ |
| 832 | return (ALL_DATA_READ); |
| 833 | } |
| 834 | continue; |
| 835 | } |
| 836 |
| 837 | /* don't have enough to complete a chunk; wait for more */ |
| 838 | if (len < req->ntoread) |
| 839 | return (MORE_DATA_EXPECTED); |
| 840 |
| 841 | /* Completed chunk */ |
| 842 | evbuffer_add(req->input_buffer, |
| 843 | EVBUFFER_DATA(buf), (size_t)req->ntoread); |
| 844 | evbuffer_drain(buf, (size_t)req->ntoread); |
| 845 | req->ntoread = -1; |
| 846 | if (req->chunk_cb != NULL) { |
| 847 | (*req->chunk_cb)(req, req->cb_arg); |
| 848 | evbuffer_drain(req->input_buffer, |
| 849 | EVBUFFER_LENGTH(req->input_buffer)); |
| 850 | } |
| 851 | } |
| 852 |
| 853 | return (MORE_DATA_EXPECTED); |
| 854 | } |
| 855 |
| 856 | static void |
| 857 | evhttp_read_trailer(struct evhttp_connection *evcon, struct evhttp_request *req) |
| 858 | { |
| 859 | struct evbuffer *buf = evcon->input_buffer; |
| 860 |
| 861 | switch (evhttp_parse_headers(req, buf)) { |
| 862 | case DATA_CORRUPTED: |
| 863 | evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER); |
| 864 | break; |
| 865 | case ALL_DATA_READ: |
| 866 | event_del(&evcon->ev); |
| 867 | evhttp_connection_done(evcon); |
| 868 | break; |
| 869 | case MORE_DATA_EXPECTED: |
| 870 | default: |
| 871 | evhttp_add_event(&evcon->ev, evcon->timeout, |
| 872 | HTTP_READ_TIMEOUT); |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 |
| 877 | static void |
| 878 | evhttp_read_body(struct evhttp_connection *evcon, struct evhttp_request *req) |
| 879 | { |
| 880 | struct evbuffer *buf = evcon->input_buffer; |
| 881 |
| 882 | if (req->chunked) { |
| 883 | switch (evhttp_handle_chunked_read(req, buf)) { |
| 884 | case ALL_DATA_READ: |
| 885 | /* finished last chunk */ |
| 886 | evcon->state = EVCON_READING_TRAILER; |
| 887 | evhttp_read_trailer(evcon, req); |
| 888 | return; |
| 889 | case DATA_CORRUPTED: |
| 890 | /* corrupted data */ |
| 891 | evhttp_connection_fail(evcon, |
| 892 | EVCON_HTTP_INVALID_HEADER); |
| 893 | return; |
| 894 | case REQUEST_CANCELED: |
| 895 | /* request canceled */ |
| 896 | evhttp_request_free(req); |
| 897 | return; |
| 898 | case MORE_DATA_EXPECTED: |
| 899 | default: |
| 900 | break; |
| 901 | } |
| 902 | } else if (req->ntoread < 0) { |
| 903 | /* Read until connection close. */ |
| 904 | evbuffer_add_buffer(req->input_buffer, buf); |
| 905 | } else if (EVBUFFER_LENGTH(buf) >= req->ntoread) { |
| 906 | /* Completed content length */ |
| 907 | evbuffer_add(req->input_buffer, EVBUFFER_DATA(buf), |
| 908 | (size_t)req->ntoread); |
| 909 | evbuffer_drain(buf, (size_t)req->ntoread); |
| 910 | req->ntoread = 0; |
| 911 | evhttp_connection_done(evcon); |
| 912 | return; |
| 913 | } |
| 914 | /* Read more! */ |
| 915 | event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read, evcon); |
| 916 | EVHTTP_BASE_SET(evcon, &evcon->ev); |
| 917 | evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT); |
| 918 | } |
| 919 |
| 920 | /* |
| 921 | * Reads data into a buffer structure until no more data |
| 922 | * can be read on the file descriptor or we have read all |
| 923 | * the data that we wanted to read. |
| 924 | * Execute callback when done. |
| 925 | */ |
| 926 |
| 927 | void |
| 928 | evhttp_read(int fd, short what, void *arg) |
| 929 | { |
| 930 | struct evhttp_connection *evcon = arg; |
| 931 | struct evhttp_request *req = TAILQ_FIRST(&evcon->requests); |
| 932 | struct evbuffer *buf = evcon->input_buffer; |
| 933 | int n, len; |
| 934 |
| 935 | if (what == EV_TIMEOUT) { |
| 936 | evhttp_connection_fail(evcon, EVCON_HTTP_TIMEOUT); |
| 937 | return; |
| 938 | } |
| 939 | n = evbuffer_read(buf, fd, -1); |
| 940 | len = EVBUFFER_LENGTH(buf); |
| 941 | event_debug(("%s: got %d on %d\n", __func__, n, fd)); |
| 942 |
| 943 | if (n == -1) { |
| 944 | if (errno != EINTR && errno != EAGAIN) { |
| 945 | event_debug(("%s: evbuffer_read", __func__)); |
| 946 | evhttp_connection_fail(evcon, EVCON_HTTP_EOF); |
| 947 | } else { |
| 948 | evhttp_add_event(&evcon->ev, evcon->timeout, |
| 949 | HTTP_READ_TIMEOUT); |
| 950 | } |
| 951 | return; |
| 952 | } else if (n == 0) { |
| 953 | /* Connection closed */ |
| 954 | evcon->state = EVCON_DISCONNECTED; |
| 955 | evhttp_connection_done(evcon); |
| 956 | return; |
| 957 | } |
| 958 |
| 959 | switch (evcon->state) { |
| 960 | case EVCON_READING_FIRSTLINE: |
| 961 | evhttp_read_firstline(evcon, req); |
| 962 | break; |
| 963 | case EVCON_READING_HEADERS: |
| 964 | evhttp_read_header(evcon, req); |
| 965 | break; |
| 966 | case EVCON_READING_BODY: |
| 967 | evhttp_read_body(evcon, req); |
| 968 | break; |
| 969 | case EVCON_READING_TRAILER: |
| 970 | evhttp_read_trailer(evcon, req); |
| 971 | break; |
| 972 | case EVCON_DISCONNECTED: |
| 973 | case EVCON_CONNECTING: |
| 974 | case EVCON_IDLE: |
| 975 | case EVCON_WRITING: |
| 976 | default: |
| 977 | event_errx(1, "%s: illegal connection state %d", |
| 978 | __func__, evcon->state); |
| 979 | } |
| 980 | } |
| 981 |
| 982 | static void |
| 983 | evhttp_write_connectioncb(struct evhttp_connection *evcon, void *arg) |
| 984 | { |
| 985 | /* This is after writing the request to the server */ |
| 986 | struct evhttp_request *req = TAILQ_FIRST(&evcon->requests); |
| 987 | assert(req != NULL); |
| 988 |
| 989 | assert(evcon->state == EVCON_WRITING); |
| 990 |
| 991 | /* We are done writing our header and are now expecting the response */ |
| 992 | req->kind = EVHTTP_RESPONSE; |
| 993 |
| 994 | evhttp_start_read(evcon); |
| 995 | } |
| 996 |
| 997 | /* |
| 998 | * Clean up a connection object |
| 999 | */ |
| 1000 |
| 1001 | void |
| 1002 | evhttp_connection_free(struct evhttp_connection *evcon) |
| 1003 | { |
| 1004 | struct evhttp_request *req; |
| 1005 |
| 1006 | /* notify interested parties that this connection is going down */ |
| 1007 | if (evcon->fd != -1) { |
| 1008 | if (evhttp_connected(evcon) && evcon->closecb != NULL) |
| 1009 | (*evcon->closecb)(evcon, evcon->closecb_arg); |
| 1010 | } |
| 1011 |
| 1012 | /* remove all requests that might be queued on this |
| 1013 | * connection. for server connections, this should be empty. |
| 1014 | * because it gets dequeued either in evhttp_connection_done or |
| 1015 | * evhttp_connection_fail. |
| 1016 | */ |
| 1017 | while ((req = TAILQ_FIRST(&evcon->requests)) != NULL) { |
| 1018 | TAILQ_REMOVE(&evcon->requests, req, next); |
| 1019 | evhttp_request_free(req); |
| 1020 | } |
| 1021 |
| 1022 | if (evcon->http_server != NULL) { |
| 1023 | struct evhttp *http = evcon->http_server; |
| 1024 | TAILQ_REMOVE(&http->connections, evcon, next); |
| 1025 | } |
| 1026 |
| 1027 | if (event_initialized(&evcon->close_ev)) |
| 1028 | event_del(&evcon->close_ev); |
| 1029 |
| 1030 | if (event_initialized(&evcon->ev)) |
| 1031 | event_del(&evcon->ev); |
| 1032 |
| 1033 | if (evcon->fd != -1) |
| 1034 | EVUTIL_CLOSESOCKET(evcon->fd); |
| 1035 |
| 1036 | if (evcon->bind_address != NULL) |
| 1037 | free(evcon->bind_address); |
| 1038 |
| 1039 | if (evcon->address != NULL) |
| 1040 | free(evcon->address); |
| 1041 |
| 1042 | if (evcon->input_buffer != NULL) |
| 1043 | evbuffer_free(evcon->input_buffer); |
| 1044 |
| 1045 | if (evcon->output_buffer != NULL) |
| 1046 | evbuffer_free(evcon->output_buffer); |
| 1047 |
| 1048 | free(evcon); |
| 1049 | } |
| 1050 |
| 1051 | void |
| 1052 | evhttp_connection_set_local_address(struct evhttp_connection *evcon, |
| 1053 | const char *address) |
| 1054 | { |
| 1055 | assert(evcon->state == EVCON_DISCONNECTED); |
| 1056 | if (evcon->bind_address) |
| 1057 | free(evcon->bind_address); |
| 1058 | if ((evcon->bind_address = strdup(address)) == NULL) |
| 1059 | event_err(1, "%s: strdup", __func__); |
| 1060 | } |
| 1061 |
| 1062 | void |
| 1063 | evhttp_connection_set_local_port(struct evhttp_connection *evcon, |
| 1064 | unsigned short port) |
| 1065 | { |
| 1066 | assert(evcon->state == EVCON_DISCONNECTED); |
| 1067 | evcon->bind_port = port; |
| 1068 | } |
| 1069 |
| 1070 | static void |
| 1071 | evhttp_request_dispatch(struct evhttp_connection* evcon) |
| 1072 | { |
| 1073 | struct evhttp_request *req = TAILQ_FIRST(&evcon->requests); |
| 1074 |
| 1075 | /* this should not usually happy but it's possible */ |
| 1076 | if (req == NULL) |
| 1077 | return; |
| 1078 |
| 1079 | /* delete possible close detection events */ |
| 1080 | evhttp_connection_stop_detectclose(evcon); |
| 1081 |
| 1082 | /* we assume that the connection is connected already */ |
| 1083 | assert(evcon->state == EVCON_IDLE); |
| 1084 |
| 1085 | evcon->state = EVCON_WRITING; |
| 1086 |
| 1087 | /* Create the header from the store arguments */ |
| 1088 | evhttp_make_header(evcon, req); |
| 1089 |
| 1090 | evhttp_write_buffer(evcon, evhttp_write_connectioncb, NULL); |
| 1091 | } |
| 1092 |
| 1093 | /* Reset our connection state */ |
| 1094 | void |
| 1095 | evhttp_connection_reset(struct evhttp_connection *evcon) |
| 1096 | { |
| 1097 | if (event_initialized(&evcon->ev)) |
| 1098 | event_del(&evcon->ev); |
| 1099 |
| 1100 | if (evcon->fd != -1) { |
| 1101 | /* inform interested parties about connection close */ |
| 1102 | if (evhttp_connected(evcon) && evcon->closecb != NULL) |
| 1103 | (*evcon->closecb)(evcon, evcon->closecb_arg); |
| 1104 |
| 1105 | EVUTIL_CLOSESOCKET(evcon->fd); |
| 1106 | evcon->fd = -1; |
| 1107 | } |
| 1108 | evcon->state = EVCON_DISCONNECTED; |
| 1109 |
| 1110 | evbuffer_drain(evcon->input_buffer, |
| 1111 | EVBUFFER_LENGTH(evcon->input_buffer)); |
| 1112 | evbuffer_drain(evcon->output_buffer, |
| 1113 | EVBUFFER_LENGTH(evcon->output_buffer)); |
| 1114 | } |
| 1115 |
| 1116 | static void |
| 1117 | evhttp_detect_close_cb(int fd, short what, void *arg) |
| 1118 | { |
| 1119 | struct evhttp_connection *evcon = arg; |
| 1120 | evhttp_connection_reset(evcon); |
| 1121 | } |
| 1122 |
| 1123 | static void |
| 1124 | evhttp_connection_start_detectclose(struct evhttp_connection *evcon) |
| 1125 | { |
| 1126 | evcon->flags |= EVHTTP_CON_CLOSEDETECT; |
| 1127 |
| 1128 | if (event_initialized(&evcon->close_ev)) |
| 1129 | event_del(&evcon->close_ev); |
| 1130 | event_set(&evcon->close_ev, evcon->fd, EV_READ, |
| 1131 | evhttp_detect_close_cb, evcon); |
| 1132 | EVHTTP_BASE_SET(evcon, &evcon->close_ev); |
| 1133 | event_add(&evcon->close_ev, NULL); |
| 1134 | } |
| 1135 |
| 1136 | static void |
| 1137 | evhttp_connection_stop_detectclose(struct evhttp_connection *evcon) |
| 1138 | { |
| 1139 | evcon->flags &= ~EVHTTP_CON_CLOSEDETECT; |
| 1140 | event_del(&evcon->close_ev); |
| 1141 | } |
| 1142 |
| 1143 | static void |
| 1144 | evhttp_connection_retry(int fd, short what, void *arg) |
| 1145 | { |
| 1146 | struct evhttp_connection *evcon = arg; |
| 1147 |
| 1148 | evcon->state = EVCON_DISCONNECTED; |
| 1149 | evhttp_connection_connect(evcon); |
| 1150 | } |
| 1151 |
| 1152 | /* |
| 1153 | * Call back for asynchronous connection attempt. |
| 1154 | */ |
| 1155 |
| 1156 | static void |
| 1157 | evhttp_connectioncb(int fd, short what, void *arg) |
| 1158 | { |
| 1159 | struct evhttp_connection *evcon = arg; |
| 1160 | int error; |
| 1161 | socklen_t errsz = sizeof(error); |
| 1162 |
| 1163 | if (what == EV_TIMEOUT) { |
| 1164 | event_debug(("%s: connection timeout for \"%s:%d\" on %d", |
| 1165 | __func__, evcon->address, evcon->port, evcon->fd)); |
| 1166 | goto cleanup; |
| 1167 | } |
| 1168 |
| 1169 | /* Check if the connection completed */ |
| 1170 | if (getsockopt(evcon->fd, SOL_SOCKET, SO_ERROR, (void*)&error, |
| 1171 | &errsz) == -1) { |
| 1172 | event_debug(("%s: getsockopt for \"%s:%d\" on %d", |
| 1173 | __func__, evcon->address, evcon->port, evcon->fd)); |
| 1174 | goto cleanup; |
| 1175 | } |
| 1176 |
| 1177 | if (error) { |
| 1178 | event_debug(("%s: connect failed for \"%s:%d\" on %d: %s", |
| 1179 | __func__, evcon->address, evcon->port, evcon->fd, |
| 1180 | strerror(error))); |
| 1181 | goto cleanup; |
| 1182 | } |
| 1183 |
| 1184 | /* We are connected to the server now */ |
| 1185 | event_debug(("%s: connected to \"%s:%d\" on %d\n", |
| 1186 | __func__, evcon->address, evcon->port, evcon->fd)); |
| 1187 |
| 1188 | /* Reset the retry count as we were successful in connecting */ |
| 1189 | evcon->retry_cnt = 0; |
| 1190 | evcon->state = EVCON_IDLE; |
| 1191 |
| 1192 | /* try to start requests that have queued up on this connection */ |
| 1193 | evhttp_request_dispatch(evcon); |
| 1194 | return; |
| 1195 |
| 1196 | cleanup: |
| 1197 | if (evcon->retry_max < 0 || evcon->retry_cnt < evcon->retry_max) { |
| 1198 | evtimer_set(&evcon->ev, evhttp_connection_retry, evcon); |
| 1199 | EVHTTP_BASE_SET(evcon, &evcon->ev); |
| 1200 | evhttp_add_event(&evcon->ev, MIN(3600, 2 << evcon->retry_cnt), |
| 1201 | HTTP_CONNECT_TIMEOUT); |
| 1202 | evcon->retry_cnt++; |
| 1203 | return; |
| 1204 | } |
| 1205 | evhttp_connection_reset(evcon); |
| 1206 |
| 1207 | /* for now, we just signal all requests by executing their callbacks */ |
| 1208 | while (TAILQ_FIRST(&evcon->requests) != NULL) { |
| 1209 | struct evhttp_request *request = TAILQ_FIRST(&evcon->requests); |
| 1210 | TAILQ_REMOVE(&evcon->requests, request, next); |
| 1211 | request->evcon = NULL; |
| 1212 |
| 1213 | /* we might want to set an error here */ |
| 1214 | request->cb(request, request->cb_arg); |
| 1215 | evhttp_request_free(request); |
| 1216 | } |
| 1217 | } |
| 1218 |
| 1219 | /* |
| 1220 | * Check if we got a valid response code. |
| 1221 | */ |
| 1222 |
| 1223 | static int |
| 1224 | evhttp_valid_response_code(int code) |
| 1225 | { |
| 1226 | if (code == 0) |
| 1227 | return (0); |
| 1228 |
| 1229 | return (1); |
| 1230 | } |
| 1231 |
| 1232 | /* Parses the status line of a web server */ |
| 1233 |
| 1234 | static int |
| 1235 | evhttp_parse_response_line(struct evhttp_request *req, char *line) |
| 1236 | { |
| 1237 | char *protocol; |
| 1238 | char *number; |
| 1239 | const char *readable = ""; |
| 1240 |
| 1241 | protocol = strsep(&line, " "); |
| 1242 | if (line == NULL) |
| 1243 | return (-1); |
| 1244 | number = strsep(&line, " "); |
| 1245 | if (line != NULL) |
| 1246 | readable = line; |
| 1247 |
| 1248 | if (strcmp(protocol, "HTTP/1.0") == 0) { |
| 1249 | req->major = 1; |
| 1250 | req->minor = 0; |
| 1251 | } else if (strcmp(protocol, "HTTP/1.1") == 0) { |
| 1252 | req->major = 1; |
| 1253 | req->minor = 1; |
| 1254 | } else { |
| 1255 | event_debug(("%s: bad protocol \"%s\"", |
| 1256 | __func__, protocol)); |
| 1257 | return (-1); |
| 1258 | } |
| 1259 |
| 1260 | req->response_code = atoi(number); |
| 1261 | if (!evhttp_valid_response_code(req->response_code)) { |
| 1262 | event_debug(("%s: bad response code \"%s\"", |
| 1263 | __func__, number)); |
| 1264 | return (-1); |
| 1265 | } |
| 1266 |
| 1267 | if ((req->response_code_line = strdup(readable)) == NULL) |
| 1268 | event_err(1, "%s: strdup", __func__); |
| 1269 |
| 1270 | return (0); |
| 1271 | } |
| 1272 |
| 1273 | /* Parse the first line of a HTTP request */ |
| 1274 |
| 1275 | static int |
| 1276 | evhttp_parse_request_line(struct evhttp_request *req, char *line) |
| 1277 | { |
| 1278 | char *method; |
| 1279 | char *uri; |
| 1280 | char *version; |
| 1281 |
| 1282 | /* Parse the request line */ |
| 1283 | method = strsep(&line, " "); |
| 1284 | if (line == NULL) |
| 1285 | return (-1); |
| 1286 | uri = strsep(&line, " "); |
| 1287 | if (line == NULL) |
| 1288 | return (-1); |
| 1289 | version = strsep(&line, " "); |
| 1290 | if (line != NULL) |
| 1291 | return (-1); |
| 1292 |
| 1293 | /* First line */ |
| 1294 | if (strcmp(method, "GET") == 0) { |
| 1295 | req->type = EVHTTP_REQ_GET; |
| 1296 | } else if (strcmp(method, "POST") == 0) { |
| 1297 | req->type = EVHTTP_REQ_POST; |
| 1298 | } else if (strcmp(method, "HEAD") == 0) { |
| 1299 | req->type = EVHTTP_REQ_HEAD; |
| 1300 | } else { |
| 1301 | event_debug(("%s: bad method %s on request %p from %s", |
| 1302 | __func__, method, req, req->remote_host)); |
| 1303 | return (-1); |
| 1304 | } |
| 1305 |
| 1306 | if (strcmp(version, "HTTP/1.0") == 0) { |
| 1307 | req->major = 1; |
| 1308 | req->minor = 0; |
| 1309 | } else if (strcmp(version, "HTTP/1.1") == 0) { |
| 1310 | req->major = 1; |
| 1311 | req->minor = 1; |
| 1312 | } else { |
| 1313 | event_debug(("%s: bad version %s on request %p from %s", |
| 1314 | __func__, version, req, req->remote_host)); |
| 1315 | return (-1); |
| 1316 | } |
| 1317 |
| 1318 | if ((req->uri = strdup(uri)) == NULL) { |
| 1319 | event_debug(("%s: strdup", __func__)); |
| 1320 | return (-1); |
| 1321 | } |
| 1322 |
| 1323 | /* determine if it's a proxy request */ |
| 1324 | if (strlen(req->uri) > 0 && req->uri[0] != '/') |
| 1325 | req->flags |= EVHTTP_PROXY_REQUEST; |
| 1326 |
| 1327 | return (0); |
| 1328 | } |
| 1329 |
| 1330 | const char * |
| 1331 | evhttp_find_header(const struct evkeyvalq *headers, const char *key) |
| 1332 | { |
| 1333 | struct evkeyval *header; |
| 1334 |
| 1335 | TAILQ_FOREACH(header, headers, next) { |
| 1336 | if (strcasecmp(header->key, key) == 0) |
| 1337 | return (header->value); |
| 1338 | } |
| 1339 |
| 1340 | return (NULL); |
| 1341 | } |
| 1342 |
| 1343 | void |
| 1344 | evhttp_clear_headers(struct evkeyvalq *headers) |
| 1345 | { |
| 1346 | struct evkeyval *header; |
| 1347 |
| 1348 | for (header = TAILQ_FIRST(headers); |
| 1349 | header != NULL; |
| 1350 | header = TAILQ_FIRST(headers)) { |
| 1351 | TAILQ_REMOVE(headers, header, next); |
| 1352 | free(header->key); |
| 1353 | free(header->value); |
| 1354 | free(header); |
| 1355 | } |
| 1356 | } |
| 1357 |
| 1358 | /* |
| 1359 | * Returns 0, if the header was successfully removed. |
| 1360 | * Returns -1, if the header could not be found. |
| 1361 | */ |
| 1362 |
| 1363 | int |
| 1364 | evhttp_remove_header(struct evkeyvalq *headers, const char *key) |
| 1365 | { |
| 1366 | struct evkeyval *header; |
| 1367 |
| 1368 | TAILQ_FOREACH(header, headers, next) { |
| 1369 | if (strcasecmp(header->key, key) == 0) |
| 1370 | break; |
| 1371 | } |
| 1372 |
| 1373 | if (header == NULL) |
| 1374 | return (-1); |
| 1375 |
| 1376 | /* Free and remove the header that we found */ |
| 1377 | TAILQ_REMOVE(headers, header, next); |
| 1378 | free(header->key); |
| 1379 | free(header->value); |
| 1380 | free(header); |
| 1381 |
| 1382 | return (0); |
| 1383 | } |
| 1384 |
| 1385 | static int |
| 1386 | evhttp_header_is_valid_value(const char *value) |
| 1387 | { |
| 1388 | const char *p = value; |
| 1389 |
| 1390 | while ((p = strpbrk(p, "\r\n")) != NULL) { |
| 1391 | /* we really expect only one new line */ |
| 1392 | p += strspn(p, "\r\n"); |
| 1393 | /* we expect a space or tab for continuation */ |
| 1394 | if (*p != ' ' && *p != '\t') |
| 1395 | return (0); |
| 1396 | } |
| 1397 | return (1); |
| 1398 | } |
| 1399 |
| 1400 | int |
| 1401 | evhttp_add_header(struct evkeyvalq *headers, |
| 1402 | const char *key, const char *value) |
| 1403 | { |
| 1404 | event_debug(("%s: key: %s val: %s\n", __func__, key, value)); |
| 1405 |
| 1406 | if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) { |
| 1407 | /* drop illegal headers */ |
| 1408 | event_debug(("%s: dropping illegal header key\n", __func__)); |
| 1409 | return (-1); |
| 1410 | } |
| 1411 |
| 1412 | if (!evhttp_header_is_valid_value(value)) { |
| 1413 | event_debug(("%s: dropping illegal header value\n", __func__)); |
| 1414 | return (-1); |
| 1415 | } |
| 1416 |
| 1417 | return (evhttp_add_header_internal(headers, key, value)); |
| 1418 | } |
| 1419 |
| 1420 | static int |
| 1421 | evhttp_add_header_internal(struct evkeyvalq *headers, |
| 1422 | const char *key, const char *value) |
| 1423 | { |
| 1424 | struct evkeyval *header = calloc(1, sizeof(struct evkeyval)); |
| 1425 | if (header == NULL) { |
| 1426 | event_warn("%s: calloc", __func__); |
| 1427 | return (-1); |
| 1428 | } |
| 1429 | if ((header->key = strdup(key)) == NULL) { |
| 1430 | free(header); |
| 1431 | event_warn("%s: strdup", __func__); |
| 1432 | return (-1); |
| 1433 | } |
| 1434 | if ((header->value = strdup(value)) == NULL) { |
| 1435 | free(header->key); |
| 1436 | free(header); |
| 1437 | event_warn("%s: strdup", __func__); |
| 1438 | return (-1); |
| 1439 | } |
| 1440 |
| 1441 | TAILQ_INSERT_TAIL(headers, header, next); |
| 1442 |
| 1443 | return (0); |
| 1444 | } |
| 1445 |
| 1446 | /* |
| 1447 | * Parses header lines from a request or a response into the specified |
| 1448 | * request object given an event buffer. |
| 1449 | * |
| 1450 | * Returns |
| 1451 | * DATA_CORRUPTED on error |
| 1452 | * MORE_DATA_EXPECTED when we need to read more headers |
| 1453 | * ALL_DATA_READ when all headers have been read. |
| 1454 | */ |
| 1455 |
| 1456 | enum message_read_status |
| 1457 | evhttp_parse_firstline(struct evhttp_request *req, struct evbuffer *buffer) |
| 1458 | { |
| 1459 | char *line; |
| 1460 | enum message_read_status status = ALL_DATA_READ; |
| 1461 |
| 1462 | line = evbuffer_readline(buffer); |
| 1463 | if (line == NULL) |
| 1464 | return (MORE_DATA_EXPECTED); |
| 1465 |
| 1466 | switch (req->kind) { |
| 1467 | case EVHTTP_REQUEST: |
| 1468 | if (evhttp_parse_request_line(req, line) == -1) |
| 1469 | status = DATA_CORRUPTED; |
| 1470 | break; |
| 1471 | case EVHTTP_RESPONSE: |
| 1472 | if (evhttp_parse_response_line(req, line) == -1) |
| 1473 | status = DATA_CORRUPTED; |
| 1474 | break; |
| 1475 | default: |
| 1476 | status = DATA_CORRUPTED; |
| 1477 | } |
| 1478 |
| 1479 | free(line); |
| 1480 | return (status); |
| 1481 | } |
| 1482 |
| 1483 | static int |
| 1484 | evhttp_append_to_last_header(struct evkeyvalq *headers, const char *line) |
| 1485 | { |
| 1486 | struct evkeyval *header = TAILQ_LAST(headers, evkeyvalq); |
| 1487 | char *newval; |
| 1488 | size_t old_len, line_len; |
| 1489 |
| 1490 | if (header == NULL) |
| 1491 | return (-1); |
| 1492 |
| 1493 | old_len = strlen(header->value); |
| 1494 | line_len = strlen(line); |
| 1495 |
| 1496 | newval = realloc(header->value, old_len + line_len + 1); |
| 1497 | if (newval == NULL) |
| 1498 | return (-1); |
| 1499 |
| 1500 | memcpy(newval + old_len, line, line_len + 1); |
| 1501 | header->value = newval; |
| 1502 |
| 1503 | return (0); |
| 1504 | } |
| 1505 |
| 1506 | enum message_read_status |
| 1507 | evhttp_parse_headers(struct evhttp_request *req, struct evbuffer* buffer) |
| 1508 | { |
| 1509 | char *line; |
| 1510 | enum message_read_status status = MORE_DATA_EXPECTED; |
| 1511 |
| 1512 | struct evkeyvalq* headers = req->input_headers; |
| 1513 | while ((line = evbuffer_readline(buffer)) |
| 1514 | != NULL) { |
| 1515 | char *skey, *svalue; |
| 1516 |
| 1517 | if (*line == '\0') { /* Last header - Done */ |
| 1518 | status = ALL_DATA_READ; |
| 1519 | free(line); |
| 1520 | break; |
| 1521 | } |
| 1522 |
| 1523 | /* Check if this is a continuation line */ |
| 1524 | if (*line == ' ' || *line == '\t') { |
| 1525 | if (evhttp_append_to_last_header(headers, line) == -1) |
| 1526 | goto error; |
| 1527 | free(line); |
| 1528 | continue; |
| 1529 | } |
| 1530 |
| 1531 | /* Processing of header lines */ |
| 1532 | svalue = line; |
| 1533 | skey = strsep(&svalue, ":"); |
| 1534 | if (svalue == NULL) |
| 1535 | goto error; |
| 1536 |
| 1537 | svalue += strspn(svalue, " "); |
| 1538 |
| 1539 | if (evhttp_add_header(headers, skey, svalue) == -1) |
| 1540 | goto error; |
| 1541 |
| 1542 | free(line); |
| 1543 | } |
| 1544 |
| 1545 | return (status); |
| 1546 |
| 1547 | error: |
| 1548 | free(line); |
| 1549 | return (DATA_CORRUPTED); |
| 1550 | } |
| 1551 |
| 1552 | static int |
| 1553 | evhttp_get_body_length(struct evhttp_request *req) |
| 1554 | { |
| 1555 | struct evkeyvalq *headers = req->input_headers; |
| 1556 | const char *content_length; |
| 1557 | const char *connection; |
| 1558 |
| 1559 | content_length = evhttp_find_header(headers, "Content-Length"); |
| 1560 | connection = evhttp_find_header(headers, "Connection"); |
| 1561 |
| 1562 | if (content_length == NULL && connection == NULL) |
| 1563 | req->ntoread = -1; |
| 1564 | else if (content_length == NULL && |
| 1565 | strcasecmp(connection, "Close") != 0) { |
| 1566 | /* Bad combination, we don't know when it will end */ |
| 1567 | event_warnx("%s: we got no content length, but the " |
| 1568 | "server wants to keep the connection open: %s.", |
| 1569 | __func__, connection); |
| 1570 | return (-1); |
| 1571 | } else if (content_length == NULL) { |
| 1572 | req->ntoread = -1; |
| 1573 | } else { |
| 1574 | char *endp; |
| 1575 | ev_int64_t ntoread = evutil_strtoll(content_length, &endp, 10); |
| 1576 | if (*content_length == '\0' || *endp != '\0' || ntoread < 0) { |
| 1577 | event_debug(("%s: illegal content length: %s", |
| 1578 | __func__, content_length)); |
| 1579 | return (-1); |
| 1580 | } |
| 1581 | req->ntoread = ntoread; |
| 1582 | } |
| 1583 |
| 1584 | event_debug(("%s: bytes to read: %lld (in buffer %ld)\n", |
| 1585 | __func__, req->ntoread, |
| 1586 | EVBUFFER_LENGTH(req->evcon->input_buffer))); |
| 1587 |
| 1588 | return (0); |
| 1589 | } |
| 1590 |
| 1591 | static void |
| 1592 | evhttp_get_body(struct evhttp_connection *evcon, struct evhttp_request *req) |
| 1593 | { |
| 1594 | const char *xfer_enc; |
| 1595 |
| 1596 | /* If this is a request without a body, then we are done */ |
| 1597 | if (req->kind == EVHTTP_REQUEST && req->type != EVHTTP_REQ_POST) { |
| 1598 | evhttp_connection_done(evcon); |
| 1599 | return; |
| 1600 | } |
| 1601 | evcon->state = EVCON_READING_BODY; |
| 1602 | xfer_enc = evhttp_find_header(req->input_headers, "Transfer-Encoding"); |
| 1603 | if (xfer_enc != NULL && strcasecmp(xfer_enc, "chunked") == 0) { |
| 1604 | req->chunked = 1; |
| 1605 | req->ntoread = -1; |
| 1606 | } else { |
| 1607 | if (evhttp_get_body_length(req) == -1) { |
| 1608 | evhttp_connection_fail(evcon, |
| 1609 | EVCON_HTTP_INVALID_HEADER); |
| 1610 | return; |
| 1611 | } |
| 1612 | } |
| 1613 | evhttp_read_body(evcon, req); |
| 1614 | } |
| 1615 |
| 1616 | static void |
| 1617 | evhttp_read_firstline(struct evhttp_connection *evcon, |
| 1618 | struct evhttp_request *req) |
| 1619 | { |
| 1620 | enum message_read_status res; |
| 1621 |
| 1622 | res = evhttp_parse_firstline(req, evcon->input_buffer); |
| 1623 | if (res == DATA_CORRUPTED) { |
| 1624 | /* Error while reading, terminate */ |
| 1625 | event_debug(("%s: bad header lines on %d\n", |
| 1626 | __func__, evcon->fd)); |
| 1627 | evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER); |
| 1628 | return; |
| 1629 | } else if (res == MORE_DATA_EXPECTED) { |
| 1630 | /* Need more header lines */ |
| 1631 | evhttp_add_event(&evcon->ev, |
| 1632 | evcon->timeout, HTTP_READ_TIMEOUT); |
| 1633 | return; |
| 1634 | } |
| 1635 |
| 1636 | evcon->state = EVCON_READING_HEADERS; |
| 1637 | evhttp_read_header(evcon, req); |
| 1638 | } |
| 1639 |
| 1640 | static void |
| 1641 | evhttp_read_header(struct evhttp_connection *evcon, struct evhttp_request *req) |
| 1642 | { |
| 1643 | enum message_read_status res; |
| 1644 | int fd = evcon->fd; |
| 1645 |
| 1646 | res = evhttp_parse_headers(req, evcon->input_buffer); |
| 1647 | if (res == DATA_CORRUPTED) { |
| 1648 | /* Error while reading, terminate */ |
| 1649 | event_debug(("%s: bad header lines on %d\n", __func__, fd)); |
| 1650 | evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER); |
| 1651 | return; |
| 1652 | } else if (res == MORE_DATA_EXPECTED) { |
| 1653 | /* Need more header lines */ |
| 1654 | evhttp_add_event(&evcon->ev, |
| 1655 | evcon->timeout, HTTP_READ_TIMEOUT); |
| 1656 | return; |
| 1657 | } |
| 1658 |
| 1659 | /* Done reading headers, do the real work */ |
| 1660 | switch (req->kind) { |
| 1661 | case EVHTTP_REQUEST: |
| 1662 | event_debug(("%s: checking for post data on %d\n", |
| 1663 | __func__, fd)); |
| 1664 | evhttp_get_body(evcon, req); |
| 1665 | break; |
| 1666 |
| 1667 | case EVHTTP_RESPONSE: |
| 1668 | if (req->response_code == HTTP_NOCONTENT || |
| 1669 | req->response_code == HTTP_NOTMODIFIED || |
| 1670 | (req->response_code >= 100 && req->response_code < 200)) { |
| 1671 | event_debug(("%s: skipping body for code %d\n", |
| 1672 | __func__, req->response_code)); |
| 1673 | evhttp_connection_done(evcon); |
| 1674 | } else { |
| 1675 | event_debug(("%s: start of read body for %s on %d\n", |
| 1676 | __func__, req->remote_host, fd)); |
| 1677 | evhttp_get_body(evcon, req); |
| 1678 | } |
| 1679 | break; |
| 1680 |
| 1681 | default: |
| 1682 | event_warnx("%s: bad header on %d", __func__, fd); |
| 1683 | evhttp_connection_fail(evcon, EVCON_HTTP_INVALID_HEADER); |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 |
| 1688 | /* |
| 1689 | * Creates a TCP connection to the specified port and executes a callback |
| 1690 | * when finished. Failure or sucess is indicate by the passed connection |
| 1691 | * object. |
| 1692 | * |
| 1693 | * Although this interface accepts a hostname, it is intended to take |
| 1694 | * only numeric hostnames so that non-blocking DNS resolution can |
| 1695 | * happen elsewhere. |
| 1696 | */ |
| 1697 |
| 1698 | struct evhttp_connection * |
| 1699 | evhttp_connection_new(const char *address, unsigned short port) |
| 1700 | { |
| 1701 | struct evhttp_connection *evcon = NULL; |
| 1702 |
| 1703 | event_debug(("Attempting connection to %s:%d\n", address, port)); |
| 1704 |
| 1705 | if ((evcon = calloc(1, sizeof(struct evhttp_connection))) == NULL) { |
| 1706 | event_warn("%s: calloc failed", __func__); |
| 1707 | goto error; |
| 1708 | } |
| 1709 |
| 1710 | evcon->fd = -1; |
| 1711 | evcon->port = port; |
| 1712 |
| 1713 | evcon->timeout = -1; |
| 1714 | evcon->retry_cnt = evcon->retry_max = 0; |
| 1715 |
| 1716 | if ((evcon->address = strdup(address)) == NULL) { |
| 1717 | event_warn("%s: strdup failed", __func__); |
| 1718 | goto error; |
| 1719 | } |
| 1720 |
| 1721 | if ((evcon->input_buffer = evbuffer_new()) == NULL) { |
| 1722 | event_warn("%s: evbuffer_new failed", __func__); |
| 1723 | goto error; |
| 1724 | } |
| 1725 |
| 1726 | if ((evcon->output_buffer = evbuffer_new()) == NULL) { |
| 1727 | event_warn("%s: evbuffer_new failed", __func__); |
| 1728 | goto error; |
| 1729 | } |
| 1730 |
| 1731 | evcon->state = EVCON_DISCONNECTED; |
| 1732 | TAILQ_INIT(&evcon->requests); |
| 1733 |
| 1734 | return (evcon); |
| 1735 |
| 1736 | error: |
| 1737 | if (evcon != NULL) |
| 1738 | evhttp_connection_free(evcon); |
| 1739 | return (NULL); |
| 1740 | } |
| 1741 |
| 1742 | void evhttp_connection_set_base(struct evhttp_connection *evcon, |
| 1743 | struct event_base *base) |
| 1744 | { |
| 1745 | assert(evcon->base == NULL); |
| 1746 | assert(evcon->state == EVCON_DISCONNECTED); |
| 1747 | evcon->base = base; |
| 1748 | } |
| 1749 |
| 1750 | void |
| 1751 | evhttp_connection_set_timeout(struct evhttp_connection *evcon, |
| 1752 | int timeout_in_secs) |
| 1753 | { |
| 1754 | evcon->timeout = timeout_in_secs; |
| 1755 | } |
| 1756 |
| 1757 | void |
| 1758 | evhttp_connection_set_retries(struct evhttp_connection *evcon, |
| 1759 | int retry_max) |
| 1760 | { |
| 1761 | evcon->retry_max = retry_max; |
| 1762 | } |
| 1763 |
| 1764 | void |
| 1765 | evhttp_connection_set_closecb(struct evhttp_connection *evcon, |
| 1766 | void (*cb)(struct evhttp_connection *, void *), void *cbarg) |
| 1767 | { |
| 1768 | evcon->closecb = cb; |
| 1769 | evcon->closecb_arg = cbarg; |
| 1770 | } |
| 1771 |
| 1772 | void |
| 1773 | evhttp_connection_get_peer(struct evhttp_connection *evcon, |
| 1774 | char **address, u_short *port) |
| 1775 | { |
| 1776 | *address = evcon->address; |
| 1777 | *port = evcon->port; |
| 1778 | } |
| 1779 |
| 1780 | int |
| 1781 | evhttp_connection_connect(struct evhttp_connection *evcon) |
| 1782 | { |
| 1783 | if (evcon->state == EVCON_CONNECTING) |
| 1784 | return (0); |
| 1785 |
| 1786 | evhttp_connection_reset(evcon); |
| 1787 |
| 1788 | assert(!(evcon->flags & EVHTTP_CON_INCOMING)); |
| 1789 | evcon->flags |= EVHTTP_CON_OUTGOING; |
| 1790 |
| 1791 | evcon->fd = bind_socket( |
| 1792 | evcon->bind_address, evcon->bind_port, 0 /*reuse*/); |
| 1793 | if (evcon->fd == -1) { |
| 1794 | event_debug(("%s: failed to bind to \"%s\"", |
| 1795 | __func__, evcon->bind_address)); |
| 1796 | return (-1); |
| 1797 | } |
| 1798 |
| 1799 | if (socket_connect(evcon->fd, evcon->address, evcon->port) == -1) { |
| 1800 | EVUTIL_CLOSESOCKET(evcon->fd); evcon->fd = -1; |
| 1801 | return (-1); |
| 1802 | } |
| 1803 |
| 1804 | /* Set up a callback for successful connection setup */ |
| 1805 | event_set(&evcon->ev, evcon->fd, EV_WRITE, evhttp_connectioncb, evcon); |
| 1806 | EVHTTP_BASE_SET(evcon, &evcon->ev); |
| 1807 | evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_CONNECT_TIMEOUT); |
| 1808 |
| 1809 | evcon->state = EVCON_CONNECTING; |
| 1810 |
| 1811 | return (0); |
| 1812 | } |
| 1813 |
| 1814 | /* |
| 1815 | * Starts an HTTP request on the provided evhttp_connection object. |
| 1816 | * If the connection object is not connected to the web server already, |
| 1817 | * this will start the connection. |
| 1818 | */ |
| 1819 |
| 1820 | int |
| 1821 | evhttp_make_request(struct evhttp_connection *evcon, |
| 1822 | struct evhttp_request *req, |
| 1823 | enum evhttp_cmd_type type, const char *uri) |
| 1824 | { |
| 1825 | /* We are making a request */ |
| 1826 | req->kind = EVHTTP_REQUEST; |
| 1827 | req->type = type; |
| 1828 | if (req->uri != NULL) |
| 1829 | free(req->uri); |
| 1830 | if ((req->uri = strdup(uri)) == NULL) |
| 1831 | event_err(1, "%s: strdup", __func__); |
| 1832 |
| 1833 | /* Set the protocol version if it is not supplied */ |
| 1834 | if (!req->major && !req->minor) { |
| 1835 | req->major = 1; |
| 1836 | req->minor = 1; |
| 1837 | } |
| 1838 |
| 1839 | assert(req->evcon == NULL); |
| 1840 | req->evcon = evcon; |
| 1841 | assert(!(req->flags & EVHTTP_REQ_OWN_CONNECTION)); |
| 1842 |
| 1843 | TAILQ_INSERT_TAIL(&evcon->requests, req, next); |
| 1844 |
| 1845 | /* If the connection object is not connected; make it so */ |
| 1846 | if (!evhttp_connected(evcon)) |
| 1847 | return (evhttp_connection_connect(evcon)); |
| 1848 |
| 1849 | /* |
| 1850 | * If it's connected already and we are the first in the queue, |
| 1851 | * then we can dispatch this request immediately. Otherwise, it |
| 1852 | * will be dispatched once the pending requests are completed. |
| 1853 | */ |
| 1854 | if (TAILQ_FIRST(&evcon->requests) == req) |
| 1855 | evhttp_request_dispatch(evcon); |
| 1856 |
| 1857 | return (0); |
| 1858 | } |
| 1859 |
| 1860 | /* |
| 1861 | * Reads data from file descriptor into request structure |
| 1862 | * Request structure needs to be set up correctly. |
| 1863 | */ |
| 1864 |
| 1865 | void |
| 1866 | evhttp_start_read(struct evhttp_connection *evcon) |
| 1867 | { |
| 1868 | /* Set up an event to read the headers */ |
| 1869 | if (event_initialized(&evcon->ev)) |
| 1870 | event_del(&evcon->ev); |
| 1871 | event_set(&evcon->ev, evcon->fd, EV_READ, evhttp_read, evcon); |
| 1872 | EVHTTP_BASE_SET(evcon, &evcon->ev); |
| 1873 |
| 1874 | evhttp_add_event(&evcon->ev, evcon->timeout, HTTP_READ_TIMEOUT); |
| 1875 | evcon->state = EVCON_READING_FIRSTLINE; |
| 1876 | } |
| 1877 |
| 1878 | static void |
| 1879 | evhttp_send_done(struct evhttp_connection *evcon, void *arg) |
| 1880 | { |
| 1881 | int need_close; |
| 1882 | struct evhttp_request *req = TAILQ_FIRST(&evcon->requests); |
| 1883 | TAILQ_REMOVE(&evcon->requests, req, next); |
| 1884 |
| 1885 | /* delete possible close detection events */ |
| 1886 | evhttp_connection_stop_detectclose(evcon); |
| 1887 |
| 1888 | need_close = |
| 1889 | (req->minor == 0 && |
| 1890 | !evhttp_is_connection_keepalive(req->input_headers))|| |
| 1891 | evhttp_is_connection_close(req->flags, req->input_headers) || |
| 1892 | evhttp_is_connection_close(req->flags, req->output_headers); |
| 1893 |
| 1894 | assert(req->flags & EVHTTP_REQ_OWN_CONNECTION); |
| 1895 | evhttp_request_free(req); |
| 1896 |
| 1897 | if (need_close) { |
| 1898 | evhttp_connection_free(evcon); |
| 1899 | return; |
| 1900 | } |
| 1901 |
| 1902 | /* we have a persistent connection; try to accept another request. */ |
| 1903 | if (evhttp_associate_new_request_with_connection(evcon) == -1) |
| 1904 | evhttp_connection_free(evcon); |
| 1905 | } |
| 1906 |
| 1907 | /* |
| 1908 | * Returns an error page. |
| 1909 | */ |
| 1910 |
| 1911 | void |
| 1912 | evhttp_send_error(struct evhttp_request *req, int error, const char *reason) |
| 1913 | { |
| 1914 | #define ERR_FORMAT "<HTML><HEAD>\n" \ |
| 1915 | "<TITLE>%d %s</TITLE>\n" \ |
| 1916 | "</HEAD><BODY>\n" \ |
| 1917 | "<H1>Method Not Implemented</H1>\n" \ |
| 1918 | "Invalid method in request<P>\n" \ |
| 1919 | "</BODY></HTML>\n" |
| 1920 |
| 1921 | struct evbuffer *buf = evbuffer_new(); |
| 1922 |
| 1923 | /* close the connection on error */ |
| 1924 | evhttp_add_header(req->output_headers, "Connection", "close"); |
| 1925 |
| 1926 | evhttp_response_code(req, error, reason); |
| 1927 |
| 1928 | evbuffer_add_printf(buf, ERR_FORMAT, error, reason); |
| 1929 |
| 1930 | evhttp_send_page(req, buf); |
| 1931 |
| 1932 | evbuffer_free(buf); |
| 1933 | #undef ERR_FORMAT |
| 1934 | } |
| 1935 |
| 1936 | /* Requires that headers and response code are already set up */ |
| 1937 |
| 1938 | static inline void |
| 1939 | evhttp_send(struct evhttp_request *req, struct evbuffer *databuf) |
| 1940 | { |
| 1941 | struct evhttp_connection *evcon = req->evcon; |
| 1942 |
| 1943 | if (evcon == NULL) { |
| 1944 | evhttp_request_free(req); |
| 1945 | return; |
| 1946 | } |
| 1947 |
| 1948 | assert(TAILQ_FIRST(&evcon->requests) == req); |
| 1949 |
| 1950 | /* we expect no more calls form the user on this request */ |
| 1951 | req->userdone = 1; |
| 1952 |
| 1953 | /* xxx: not sure if we really should expose the data buffer this way */ |
| 1954 | if (databuf != NULL) |
| 1955 | evbuffer_add_buffer(req->output_buffer, databuf); |
| 1956 |
| 1957 | /* Adds headers to the response */ |
| 1958 | evhttp_make_header(evcon, req); |
| 1959 |
| 1960 | evhttp_write_buffer(evcon, evhttp_send_done, NULL); |
| 1961 | } |
| 1962 |
| 1963 | void |
| 1964 | evhttp_send_reply(struct evhttp_request *req, int code, const char *reason, |
| 1965 | struct evbuffer *databuf) |
| 1966 | { |
| 1967 | evhttp_response_code(req, code, reason); |
| 1968 |
| 1969 | evhttp_send(req, databuf); |
| 1970 | } |
| 1971 |
| 1972 | void |
| 1973 | evhttp_send_reply_start(struct evhttp_request *req, int code, |
| 1974 | const char *reason) |
| 1975 | { |
| 1976 | evhttp_response_code(req, code, reason); |
| 1977 | if (req->major == 1 && req->minor == 1) { |
| 1978 | /* use chunked encoding for HTTP/1.1 */ |
| 1979 | evhttp_add_header(req->output_headers, "Transfer-Encoding", |
| 1980 | "chunked"); |
| 1981 | req->chunked = 1; |
| 1982 | } |
| 1983 | evhttp_make_header(req->evcon, req); |
| 1984 | evhttp_write_buffer(req->evcon, NULL, NULL); |
| 1985 | } |
| 1986 |
| 1987 | void |
| 1988 | evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf) |
| 1989 | { |
| 1990 | struct evhttp_connection *evcon = req->evcon; |
| 1991 |
| 1992 | if (evcon == NULL) |
| 1993 | return; |
| 1994 |
| 1995 | if (req->chunked) { |
| 1996 | evbuffer_add_printf(evcon->output_buffer, "%x\r\n", |
| 1997 | (unsigned)EVBUFFER_LENGTH(databuf)); |
| 1998 | } |
| 1999 | evbuffer_add_buffer(evcon->output_buffer, databuf); |
| 2000 | if (req->chunked) { |
| 2001 | evbuffer_add(evcon->output_buffer, "\r\n", 2); |
| 2002 | } |
| 2003 | evhttp_write_buffer(evcon, NULL, NULL); |
| 2004 | } |
| 2005 |
| 2006 | void |
| 2007 | evhttp_send_reply_end(struct evhttp_request *req) |
| 2008 | { |
| 2009 | struct evhttp_connection *evcon = req->evcon; |
| 2010 |
| 2011 | if (evcon == NULL) { |
| 2012 | evhttp_request_free(req); |
| 2013 | return; |
| 2014 | } |
| 2015 |
| 2016 | /* we expect no more calls form the user on this request */ |
| 2017 | req->userdone = 1; |
| 2018 |
| 2019 | if (req->chunked) { |
| 2020 | evbuffer_add(req->evcon->output_buffer, "0\r\n\r\n", 5); |
| 2021 | evhttp_write_buffer(req->evcon, evhttp_send_done, NULL); |
| 2022 | req->chunked = 0; |
| 2023 | } else if (!event_pending(&evcon->ev, EV_WRITE|EV_TIMEOUT, NULL)) { |
| 2024 | /* let the connection know that we are done with the request */ |
| 2025 | evhttp_send_done(evcon, NULL); |
| 2026 | } else { |
| 2027 | /* make the callback execute after all data has been written */ |
| 2028 | evcon->cb = evhttp_send_done; |
| 2029 | evcon->cb_arg = NULL; |
| 2030 | } |
| 2031 | } |
| 2032 |
| 2033 | void |
| 2034 | evhttp_response_code(struct evhttp_request *req, int code, const char *reason) |
| 2035 | { |
| 2036 | req->kind = EVHTTP_RESPONSE; |
| 2037 | req->response_code = code; |
| 2038 | if (req->response_code_line != NULL) |
| 2039 | free(req->response_code_line); |
| 2040 | req->response_code_line = strdup(reason); |
| 2041 | } |
| 2042 |
| 2043 | void |
| 2044 | evhttp_send_page(struct evhttp_request *req, struct evbuffer *databuf) |
| 2045 | { |
| 2046 | if (!req->major || !req->minor) { |
| 2047 | req->major = 1; |
| 2048 | req->minor = 1; |
| 2049 | } |
| 2050 |
| 2051 | if (req->kind != EVHTTP_RESPONSE) |
| 2052 | evhttp_response_code(req, 200, "OK"); |
| 2053 |
| 2054 | evhttp_clear_headers(req->output_headers); |
| 2055 | evhttp_add_header(req->output_headers, "Content-Type", "text/html"); |
| 2056 | evhttp_add_header(req->output_headers, "Connection", "close"); |
| 2057 |
| 2058 | evhttp_send(req, databuf); |
| 2059 | } |
| 2060 |
| 2061 | static const char uri_chars[256] = { |
| 2062 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2063 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2064 | 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2065 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, |
| 2066 | /* 64 */ |
| 2067 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2068 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, |
| 2069 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 2070 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, |
| 2071 | /* 128 */ |
| 2072 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2073 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2074 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2075 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2076 | /* 192 */ |
| 2077 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2078 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2079 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2080 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2081 | }; |
| 2082 |
| 2083 | /* |
| 2084 | * Helper functions to encode/decode a URI. |
| 2085 | * The returned string must be freed by the caller. |
| 2086 | */ |
| 2087 | char * |
| 2088 | evhttp_encode_uri(const char *uri) |
| 2089 | { |
| 2090 | struct evbuffer *buf = evbuffer_new(); |
| 2091 | char *p; |
| 2092 |
| 2093 | for (p = (char *)uri; *p != '\0'; p++) { |
| 2094 | if (uri_chars[(u_char)(*p)]) { |
| 2095 | evbuffer_add(buf, p, 1); |
| 2096 | } else { |
| 2097 | evbuffer_add_printf(buf, "%%%02X", (u_char)(*p)); |
| 2098 | } |
| 2099 | } |
| 2100 | evbuffer_add(buf, "", 1); |
| 2101 | p = strdup((char *)EVBUFFER_DATA(buf)); |
| 2102 | evbuffer_free(buf); |
| 2103 |
| 2104 | return (p); |
| 2105 | } |
| 2106 |
| 2107 | /* |
| 2108 | * @param always_decode_plus: when true we transform plus to space even |
| 2109 | * if we have not seen a ?. |
| 2110 | */ |
| 2111 | static int |
| 2112 | evhttp_decode_uri_internal( |
| 2113 | const char *uri, size_t length, char *ret, int always_decode_plus) |
| 2114 | { |
| 2115 | char c; |
| 2116 | int i, j, in_query = always_decode_plus; |
| 2117 |
| 2118 | for (i = j = 0; uri[i] != '\0'; i++) { |
| 2119 | c = uri[i]; |
| 2120 | if (c == '?') { |
| 2121 | in_query = 1; |
| 2122 | } else if (c == '+' && in_query) { |
| 2123 | c = ' '; |
| 2124 | } else if (c == '%' && isxdigit((unsigned char)uri[i+1]) && |
| 2125 | isxdigit((unsigned char)uri[i+2])) { |
| 2126 | char tmp[] = { uri[i+1], uri[i+2], '\0' }; |
| 2127 | c = (char)strtol(tmp, NULL, 16); |
| 2128 | i += 2; |
| 2129 | } |
| 2130 | ret[j++] = c; |
| 2131 | } |
| 2132 | ret[j] = '\0'; |
| 2133 |
| 2134 | return (j); |
| 2135 | } |
| 2136 |
| 2137 | char * |
| 2138 | evhttp_decode_uri(const char *uri) |
| 2139 | { |
| 2140 | char *ret; |
| 2141 |
| 2142 | if ((ret = malloc(strlen(uri) + 1)) == NULL) |
| 2143 | event_err(1, "%s: malloc(%lu)", __func__, |
| 2144 | (unsigned long)(strlen(uri) + 1)); |
| 2145 |
| 2146 | evhttp_decode_uri_internal(uri, strlen(uri), |
| 2147 | ret, 0 /*always_decode_plus*/); |
| 2148 |
| 2149 | return (ret); |
| 2150 | } |
| 2151 |
| 2152 | /* |
| 2153 | * Helper function to parse out arguments in a query. |
| 2154 | * The arguments are separated by key and value. |
| 2155 | */ |
| 2156 |
| 2157 | void |
| 2158 | evhttp_parse_query(const char *uri, struct evkeyvalq *headers) |
| 2159 | { |
| 2160 | char *line; |
| 2161 | char *argument; |
| 2162 | char *p; |
| 2163 |
| 2164 | TAILQ_INIT(headers); |
| 2165 |
| 2166 | /* No arguments - we are done */ |
| 2167 | if (strchr(uri, '?') == NULL) |
| 2168 | return; |
| 2169 |
| 2170 | if ((line = strdup(uri)) == NULL) |
| 2171 | event_err(1, "%s: strdup", __func__); |
| 2172 |
| 2173 |
| 2174 | argument = line; |
| 2175 |
| 2176 | /* We already know that there has to be a ? */ |
| 2177 | strsep(&argument, "?"); |
| 2178 |
| 2179 | p = argument; |
| 2180 | while (p != NULL && *p != '\0') { |
| 2181 | char *key, *value, *decoded_value; |
| 2182 | argument = strsep(&p, "&"); |
| 2183 |
| 2184 | value = argument; |
| 2185 | key = strsep(&value, "="); |
| 2186 | if (value == NULL) |
| 2187 | goto error; |
| 2188 |
| 2189 | if ((decoded_value = malloc(strlen(value) + 1)) == NULL) |
| 2190 | event_err(1, "%s: malloc", __func__); |
| 2191 |
| 2192 | evhttp_decode_uri_internal(value, strlen(value), |
| 2193 | decoded_value, 1 /*always_decode_plus*/); |
| 2194 | event_debug(("Query Param: %s -> %s\n", key, decoded_value)); |
| 2195 | evhttp_add_header_internal(headers, key, decoded_value); |
| 2196 | free(decoded_value); |
| 2197 | } |
| 2198 |
| 2199 | error: |
| 2200 | free(line); |
| 2201 | } |
| 2202 |
| 2203 | static struct evhttp_cb * |
| 2204 | evhttp_dispatch_callback(struct httpcbq *callbacks, struct evhttp_request *req) |
| 2205 | { |
| 2206 | struct evhttp_cb *cb; |
| 2207 | size_t offset = 0; |
| 2208 |
| 2209 | /* Test for different URLs */ |
| 2210 | char *p = strchr(req->uri, '?'); |
| 2211 | if (p != NULL) |
| 2212 | offset = (size_t)(p - req->uri); |
| 2213 |
| 2214 | TAILQ_FOREACH(cb, callbacks, next) { |
| 2215 | int res = 0; |
| 2216 | if (p == NULL) { |
| 2217 | res = strcmp(cb->what, req->uri) == 0; |
| 2218 | } else { |
| 2219 | res = ((strncmp(cb->what, req->uri, offset) == 0) && |
| 2220 | (cb->what[offset] == '\0')); |
| 2221 | } |
| 2222 |
| 2223 | if (res) |
| 2224 | return (cb); |
| 2225 | } |
| 2226 |
| 2227 | return (NULL); |
| 2228 | } |
| 2229 |
| 2230 | static void |
| 2231 | evhttp_handle_request(struct evhttp_request *req, void *arg) |
| 2232 | { |
| 2233 | struct evhttp *http = arg; |
| 2234 | struct evhttp_cb *cb = NULL; |
| 2235 |
| 2236 | event_debug(("%s: req->uri=%s", __func__, req->uri)); |
| 2237 | if (req->uri == NULL) { |
| 2238 | event_debug(("%s: bad request", __func__)); |
| 2239 | if (req->evcon->state == EVCON_DISCONNECTED) { |
| 2240 | evhttp_connection_fail(req->evcon, EVCON_HTTP_EOF); |
| 2241 | } else { |
| 2242 | event_debug(("%s: sending error", __func__)); |
| 2243 | evhttp_send_error(req, HTTP_BADREQUEST, "Bad Request"); |
| 2244 | } |
| 2245 | return; |
| 2246 | } |
| 2247 |
| 2248 | if ((cb = evhttp_dispatch_callback(&http->callbacks, req)) != NULL) { |
| 2249 | (*cb->cb)(req, cb->cbarg); |
| 2250 | return; |
| 2251 | } |
| 2252 |
| 2253 | /* Generic call back */ |
| 2254 | if (http->gencb) { |
| 2255 | (*http->gencb)(req, http->gencbarg); |
| 2256 | return; |
| 2257 | } else { |
| 2258 | /* We need to send a 404 here */ |
| 2259 | #define ERR_FORMAT "<html><head>" \ |
| 2260 | "<title>404 Not Found</title>" \ |
| 2261 | "</head><body>" \ |
| 2262 | "<h1>Not Found</h1>" \ |
| 2263 | "<p>The requested URL %s was not found on this server.</p>"\ |
| 2264 | "</body></html>\n" |
| 2265 |
| 2266 | char *escaped_html = evhttp_htmlescape(req->uri); |
| 2267 | struct evbuffer *buf = evbuffer_new(); |
| 2268 |
| 2269 | evhttp_response_code(req, HTTP_NOTFOUND, "Not Found"); |
| 2270 |
| 2271 | evbuffer_add_printf(buf, ERR_FORMAT, escaped_html); |
| 2272 |
| 2273 | free(escaped_html); |
| 2274 |
| 2275 | evhttp_send_page(req, buf); |
| 2276 |
| 2277 | evbuffer_free(buf); |
| 2278 | #undef ERR_FORMAT |
| 2279 | } |
| 2280 | } |
| 2281 |
| 2282 | static void |
| 2283 | accept_socket(int fd, short what, void *arg) |
| 2284 | { |
| 2285 | struct evhttp *http = arg; |
| 2286 | struct sockaddr_storage ss; |
| 2287 | socklen_t addrlen = sizeof(ss); |
| 2288 | int nfd; |
| 2289 |
| 2290 | if ((nfd = accept(fd, (struct sockaddr *)&ss, &addrlen)) == -1) { |
| 2291 | if (errno != EAGAIN && errno != EINTR) |
| 2292 | event_warn("%s: bad accept", __func__); |
| 2293 | return; |
| 2294 | } |
| 2295 | if (evutil_make_socket_nonblocking(nfd) < 0) |
| 2296 | return; |
| 2297 |
| 2298 | evhttp_get_request(http, nfd, (struct sockaddr *)&ss, addrlen); |
| 2299 | } |
| 2300 |
| 2301 | int |
| 2302 | evhttp_bind_socket(struct evhttp *http, const char *address, u_short port) |
| 2303 | { |
| 2304 | int fd; |
| 2305 | int res; |
| 2306 |
| 2307 | if ((fd = bind_socket(address, port, 1 /*reuse*/)) == -1) |
| 2308 | return (-1); |
| 2309 |
| 2310 | if (listen(fd, 128) == -1) { |
| 2311 | event_warn("%s: listen", __func__); |
| 2312 | EVUTIL_CLOSESOCKET(fd); |
| 2313 | return (-1); |
| 2314 | } |
| 2315 |
| 2316 | res = evhttp_accept_socket(http, fd); |
| 2317 |
| 2318 | if (res != -1) |
| 2319 | event_debug(("Bound to port %d - Awaiting connections ... ", |
| 2320 | port)); |
| 2321 |
| 2322 | return (res); |
| 2323 | } |
| 2324 |
| 2325 | int |
| 2326 | evhttp_accept_socket(struct evhttp *http, int fd) |
| 2327 | { |
| 2328 | struct evhttp_bound_socket *bound; |
| 2329 | struct event *ev; |
| 2330 | int res; |
| 2331 |
| 2332 | bound = malloc(sizeof(struct evhttp_bound_socket)); |
| 2333 | if (bound == NULL) |
| 2334 | return (-1); |
| 2335 |
| 2336 | ev = &bound->bind_ev; |
| 2337 |
| 2338 | /* Schedule the socket for accepting */ |
| 2339 | event_set(ev, fd, EV_READ | EV_PERSIST, accept_socket, http); |
| 2340 | EVHTTP_BASE_SET(http, ev); |
| 2341 |
| 2342 | res = event_add(ev, NULL); |
| 2343 |
| 2344 | if (res == -1) { |
| 2345 | free(bound); |
| 2346 | return (-1); |
| 2347 | } |
| 2348 |
| 2349 | TAILQ_INSERT_TAIL(&http->sockets, bound, next); |
| 2350 |
| 2351 | return (0); |
| 2352 | } |
| 2353 |
| 2354 | static struct evhttp* |
| 2355 | evhttp_new_object(void) |
| 2356 | { |
| 2357 | struct evhttp *http = NULL; |
| 2358 |
| 2359 | if ((http = calloc(1, sizeof(struct evhttp))) == NULL) { |
| 2360 | event_warn("%s: calloc", __func__); |
| 2361 | return (NULL); |
| 2362 | } |
| 2363 |
| 2364 | http->timeout = -1; |
| 2365 |
| 2366 | TAILQ_INIT(&http->sockets); |
| 2367 | TAILQ_INIT(&http->callbacks); |
| 2368 | TAILQ_INIT(&http->connections); |
| 2369 |
| 2370 | return (http); |
| 2371 | } |
| 2372 |
| 2373 | struct evhttp * |
| 2374 | evhttp_new(struct event_base *base) |
| 2375 | { |
| 2376 | struct evhttp *http = evhttp_new_object(); |
| 2377 |
| 2378 | http->base = base; |
| 2379 |
| 2380 | return (http); |
| 2381 | } |
| 2382 |
| 2383 | /* |
| 2384 | * Start a web server on the specified address and port. |
| 2385 | */ |
| 2386 |
| 2387 | struct evhttp * |
| 2388 | evhttp_start(const char *address, u_short port) |
| 2389 | { |
| 2390 | struct evhttp *http = evhttp_new_object(); |
| 2391 |
| 2392 | if (evhttp_bind_socket(http, address, port) == -1) { |
| 2393 | free(http); |
| 2394 | return (NULL); |
| 2395 | } |
| 2396 |
| 2397 | return (http); |
| 2398 | } |
| 2399 |
| 2400 | void |
| 2401 | evhttp_free(struct evhttp* http) |
| 2402 | { |
| 2403 | struct evhttp_cb *http_cb; |
| 2404 | struct evhttp_connection *evcon; |
| 2405 | struct evhttp_bound_socket *bound; |
| 2406 | int fd; |
| 2407 |
| 2408 | /* Remove the accepting part */ |
| 2409 | while ((bound = TAILQ_FIRST(&http->sockets)) != NULL) { |
| 2410 | TAILQ_REMOVE(&http->sockets, bound, next); |
| 2411 |
| 2412 | fd = bound->bind_ev.ev_fd; |
| 2413 | event_del(&bound->bind_ev); |
| 2414 | EVUTIL_CLOSESOCKET(fd); |
| 2415 |
| 2416 | free(bound); |
| 2417 | } |
| 2418 |
| 2419 | while ((evcon = TAILQ_FIRST(&http->connections)) != NULL) { |
| 2420 | /* evhttp_connection_free removes the connection */ |
| 2421 | evhttp_connection_free(evcon); |
| 2422 | } |
| 2423 |
| 2424 | while ((http_cb = TAILQ_FIRST(&http->callbacks)) != NULL) { |
| 2425 | TAILQ_REMOVE(&http->callbacks, http_cb, next); |
| 2426 | free(http_cb->what); |
| 2427 | free(http_cb); |
| 2428 | } |
| 2429 |
| 2430 | free(http); |
| 2431 | } |
| 2432 |
| 2433 | void |
| 2434 | evhttp_set_timeout(struct evhttp* http, int timeout_in_secs) |
| 2435 | { |
| 2436 | http->timeout = timeout_in_secs; |
| 2437 | } |
| 2438 |
| 2439 | void |
| 2440 | evhttp_set_cb(struct evhttp *http, const char *uri, |
| 2441 | void (*cb)(struct evhttp_request *, void *), void *cbarg) |
| 2442 | { |
| 2443 | struct evhttp_cb *http_cb; |
| 2444 |
| 2445 | if ((http_cb = calloc(1, sizeof(struct evhttp_cb))) == NULL) |
| 2446 | event_err(1, "%s: calloc", __func__); |
| 2447 |
| 2448 | http_cb->what = strdup(uri); |
| 2449 | http_cb->cb = cb; |
| 2450 | http_cb->cbarg = cbarg; |
| 2451 |
| 2452 | TAILQ_INSERT_TAIL(&http->callbacks, http_cb, next); |
| 2453 | } |
| 2454 |
| 2455 | int |
| 2456 | evhttp_del_cb(struct evhttp *http, const char *uri) |
| 2457 | { |
| 2458 | struct evhttp_cb *http_cb; |
| 2459 |
| 2460 | TAILQ_FOREACH(http_cb, &http->callbacks, next) { |
| 2461 | if (strcmp(http_cb->what, uri) == 0) |
| 2462 | break; |
| 2463 | } |
| 2464 | if (http_cb == NULL) |
| 2465 | return (-1); |
| 2466 |
| 2467 | TAILQ_REMOVE(&http->callbacks, http_cb, next); |
| 2468 | free(http_cb->what); |
| 2469 | free(http_cb); |
| 2470 |
| 2471 | return (0); |
| 2472 | } |
| 2473 |
| 2474 | void |
| 2475 | evhttp_set_gencb(struct evhttp *http, |
| 2476 | void (*cb)(struct evhttp_request *, void *), void *cbarg) |
| 2477 | { |
| 2478 | http->gencb = cb; |
| 2479 | http->gencbarg = cbarg; |
| 2480 | } |
| 2481 |
| 2482 | /* |
| 2483 | * Request related functions |
| 2484 | */ |
| 2485 |
| 2486 | struct evhttp_request * |
| 2487 | evhttp_request_new(void (*cb)(struct evhttp_request *, void *), void *arg) |
| 2488 | { |
| 2489 | struct evhttp_request *req = NULL; |
| 2490 |
| 2491 | /* Allocate request structure */ |
| 2492 | if ((req = calloc(1, sizeof(struct evhttp_request))) == NULL) { |
| 2493 | event_warn("%s: calloc", __func__); |
| 2494 | goto error; |
| 2495 | } |
| 2496 |
| 2497 | req->kind = EVHTTP_RESPONSE; |
| 2498 | req->input_headers = calloc(1, sizeof(struct evkeyvalq)); |
| 2499 | if (req->input_headers == NULL) { |
| 2500 | event_warn("%s: calloc", __func__); |
| 2501 | goto error; |
| 2502 | } |
| 2503 | TAILQ_INIT(req->input_headers); |
| 2504 |
| 2505 | req->output_headers = calloc(1, sizeof(struct evkeyvalq)); |
| 2506 | if (req->output_headers == NULL) { |
| 2507 | event_warn("%s: calloc", __func__); |
| 2508 | goto error; |
| 2509 | } |
| 2510 | TAILQ_INIT(req->output_headers); |
| 2511 |
| 2512 | if ((req->input_buffer = evbuffer_new()) == NULL) { |
| 2513 | event_warn("%s: evbuffer_new", __func__); |
| 2514 | goto error; |
| 2515 | } |
| 2516 |
| 2517 | if ((req->output_buffer = evbuffer_new()) == NULL) { |
| 2518 | event_warn("%s: evbuffer_new", __func__); |
| 2519 | goto error; |
| 2520 | } |
| 2521 |
| 2522 | req->cb = cb; |
| 2523 | req->cb_arg = arg; |
| 2524 |
| 2525 | return (req); |
| 2526 |
| 2527 | error: |
| 2528 | if (req != NULL) |
| 2529 | evhttp_request_free(req); |
| 2530 | return (NULL); |
| 2531 | } |
| 2532 |
| 2533 | void |
| 2534 | evhttp_request_free(struct evhttp_request *req) |
| 2535 | { |
| 2536 | if (req->remote_host != NULL) |
| 2537 | free(req->remote_host); |
| 2538 | if (req->uri != NULL) |
| 2539 | free(req->uri); |
| 2540 | if (req->response_code_line != NULL) |
| 2541 | free(req->response_code_line); |
| 2542 |
| 2543 | evhttp_clear_headers(req->input_headers); |
| 2544 | free(req->input_headers); |
| 2545 |
| 2546 | evhttp_clear_headers(req->output_headers); |
| 2547 | free(req->output_headers); |
| 2548 |
| 2549 | if (req->input_buffer != NULL) |
| 2550 | evbuffer_free(req->input_buffer); |
| 2551 |
| 2552 | if (req->output_buffer != NULL) |
| 2553 | evbuffer_free(req->output_buffer); |
| 2554 |
| 2555 | free(req); |
| 2556 | } |
| 2557 |
| 2558 | struct evhttp_connection * |
| 2559 | evhttp_request_get_connection(struct evhttp_request *req) |
| 2560 | { |
| 2561 | return req->evcon; |
| 2562 | } |
| 2563 |
| 2564 |
| 2565 | void |
| 2566 | evhttp_request_set_chunked_cb(struct evhttp_request *req, |
| 2567 | void (*cb)(struct evhttp_request *, void *)) |
| 2568 | { |
| 2569 | req->chunk_cb = cb; |
| 2570 | } |
| 2571 |
| 2572 | /* |
| 2573 | * Allows for inspection of the request URI |
| 2574 | */ |
| 2575 |
| 2576 | const char * |
| 2577 | evhttp_request_uri(struct evhttp_request *req) { |
| 2578 | if (req->uri == NULL) |
| 2579 | event_debug(("%s: request %p has no uri\n", __func__, req)); |
| 2580 | return (req->uri); |
| 2581 | } |
| 2582 |
| 2583 | /* |
| 2584 | * Takes a file descriptor to read a request from. |
| 2585 | * The callback is executed once the whole request has been read. |
| 2586 | */ |
| 2587 |
| 2588 | static struct evhttp_connection* |
| 2589 | evhttp_get_request_connection( |
| 2590 | struct evhttp* http, |
| 2591 | int fd, struct sockaddr *sa, socklen_t salen) |
| 2592 | { |
| 2593 | struct evhttp_connection *evcon; |
| 2594 | char *hostname = NULL, *portname = NULL; |
| 2595 |
| 2596 | name_from_addr(sa, salen, &hostname, &portname); |
| 2597 | if (hostname == NULL || portname == NULL) { |
| 2598 | if (hostname) free(hostname); |
| 2599 | if (portname) free(portname); |
| 2600 | return (NULL); |
| 2601 | } |
| 2602 |
| 2603 | event_debug(("%s: new request from %s:%s on %d\n", |
| 2604 | __func__, hostname, portname, fd)); |
| 2605 |
| 2606 | /* we need a connection object to put the http request on */ |
| 2607 | evcon = evhttp_connection_new(hostname, atoi(portname)); |
| 2608 | free(hostname); |
| 2609 | free(portname); |
| 2610 | if (evcon == NULL) |
| 2611 | return (NULL); |
| 2612 |
| 2613 | /* associate the base if we have one*/ |
| 2614 | evhttp_connection_set_base(evcon, http->base); |
| 2615 |
| 2616 | evcon->flags |= EVHTTP_CON_INCOMING; |
| 2617 | evcon->state = EVCON_READING_FIRSTLINE; |
| 2618 |
| 2619 | evcon->fd = fd; |
| 2620 |
| 2621 | return (evcon); |
| 2622 | } |
| 2623 |
| 2624 | static int |
| 2625 | evhttp_associate_new_request_with_connection(struct evhttp_connection *evcon) |
| 2626 | { |
| 2627 | struct evhttp *http = evcon->http_server; |
| 2628 | struct evhttp_request *req; |
| 2629 | if ((req = evhttp_request_new(evhttp_handle_request, http)) == NULL) |
| 2630 | return (-1); |
| 2631 |
| 2632 | req->evcon = evcon; /* the request ends up owning the connection */ |
| 2633 | req->flags |= EVHTTP_REQ_OWN_CONNECTION; |
| 2634 |
| 2635 | TAILQ_INSERT_TAIL(&evcon->requests, req, next); |
| 2636 |
| 2637 | req->kind = EVHTTP_REQUEST; |
| 2638 |
| 2639 | if ((req->remote_host = strdup(evcon->address)) == NULL) |
| 2640 | event_err(1, "%s: strdup", __func__); |
| 2641 | req->remote_port = evcon->port; |
| 2642 |
| 2643 | evhttp_start_read(evcon); |
| 2644 |
| 2645 | return (0); |
| 2646 | } |
| 2647 |
| 2648 | void |
| 2649 | evhttp_get_request(struct evhttp *http, int fd, |
| 2650 | struct sockaddr *sa, socklen_t salen) |
| 2651 | { |
| 2652 | struct evhttp_connection *evcon; |
| 2653 |
| 2654 | evcon = evhttp_get_request_connection(http, fd, sa, salen); |
| 2655 | if (evcon == NULL) |
| 2656 | return; |
| 2657 |
| 2658 | /* the timeout can be used by the server to close idle connections */ |
| 2659 | if (http->timeout != -1) |
| 2660 | evhttp_connection_set_timeout(evcon, http->timeout); |
| 2661 |
| 2662 | /* |
| 2663 | * if we want to accept more than one request on a connection, |
| 2664 | * we need to know which http server it belongs to. |
| 2665 | */ |
| 2666 | evcon->http_server = http; |
| 2667 | TAILQ_INSERT_TAIL(&http->connections, evcon, next); |
| 2668 |
| 2669 | if (evhttp_associate_new_request_with_connection(evcon) == -1) |
| 2670 | evhttp_connection_free(evcon); |
| 2671 | } |
| 2672 |
| 2673 |
| 2674 | /* |
| 2675 | * Network helper functions that we do not want to export to the rest of |
| 2676 | * the world. |
| 2677 | */ |
| 2678 | #if 0 /* Unused */ |
| 2679 | static struct addrinfo * |
| 2680 | addr_from_name(char *address) |
| 2681 | { |
| 2682 | #ifdef HAVE_GETADDRINFO |
| 2683 | struct addrinfo ai, *aitop; |
| 2684 | int ai_result; |
| 2685 |
| 2686 | memset(&ai, 0, sizeof(ai)); |
| 2687 | ai.ai_family = AF_INET; |
| 2688 | ai.ai_socktype = SOCK_RAW; |
| 2689 | ai.ai_flags = 0; |
| 2690 | if ((ai_result = getaddrinfo(address, NULL, &ai, &aitop)) != 0) { |
| 2691 | if ( ai_result == EAI_SYSTEM ) |
| 2692 | event_warn("getaddrinfo"); |
| 2693 | else |
| 2694 | event_warnx("getaddrinfo: %s", gai_strerror(ai_result)); |
| 2695 | } |
| 2696 |
| 2697 | return (aitop); |
| 2698 | #else |
| 2699 | assert(0); |
| 2700 | return NULL; /* XXXXX Use gethostbyname, if this function is ever used. */ |
| 2701 | #endif |
| 2702 | } |
| 2703 | #endif |
| 2704 |
| 2705 | static void |
| 2706 | name_from_addr(struct sockaddr *sa, socklen_t salen, |
| 2707 | char **phost, char **pport) |
| 2708 | { |
| 2709 | char ntop[NI_MAXHOST]; |
| 2710 | char strport[NI_MAXSERV]; |
| 2711 | int ni_result; |
| 2712 |
| 2713 | #ifdef HAVE_GETNAMEINFO |
| 2714 | ni_result = getnameinfo(sa, salen, |
| 2715 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 2716 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 2717 |
| 2718 | if (ni_result != 0) { |
| 2719 | if (ni_result == EAI_SYSTEM) |
| 2720 | event_err(1, "getnameinfo failed"); |
| 2721 | else |
| 2722 | event_errx(1, "getnameinfo failed: %s", gai_strerror(ni_result)); |
| 2723 | return; |
| 2724 | } |
| 2725 | #else |
| 2726 | ni_result = fake_getnameinfo(sa, salen, |
| 2727 | ntop, sizeof(ntop), strport, sizeof(strport), |
| 2728 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 2729 | if (ni_result != 0) |
| 2730 | return; |
| 2731 | #endif |
| 2732 | *phost = strdup(ntop); |
| 2733 | *pport = strdup(strport); |
| 2734 | } |
| 2735 |
| 2736 | /* Create a non-blocking socket and bind it */ |
| 2737 | /* todo: rename this function */ |
| 2738 | static int |
| 2739 | bind_socket_ai(struct addrinfo *ai, int reuse) |
| 2740 | { |
| 2741 | int fd, on = 1, r; |
| 2742 | int serrno; |
| 2743 |
| 2744 | /* Create listen socket */ |
| 2745 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 2746 | if (fd == -1) { |
| 2747 | event_warn("socket"); |
| 2748 | return (-1); |
| 2749 | } |
| 2750 |
| 2751 | if (evutil_make_socket_nonblocking(fd) < 0) |
| 2752 | goto out; |
| 2753 |
| 2754 | #ifndef WIN32 |
| 2755 | if (fcntl(fd, F_SETFD, 1) == -1) { |
| 2756 | event_warn("fcntl(F_SETFD)"); |
| 2757 | goto out; |
| 2758 | } |
| 2759 | #endif |
| 2760 |
| 2761 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on)); |
| 2762 | if (reuse) { |
| 2763 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 2764 | (void *)&on, sizeof(on)); |
| 2765 | } |
| 2766 |
| 2767 | if (ai != NULL) { |
| 2768 | r = bind(fd, ai->ai_addr, ai->ai_addrlen); |
| 2769 | if (r == -1) |
| 2770 | goto out; |
| 2771 | } |
| 2772 |
| 2773 | return (fd); |
| 2774 |
| 2775 | out: |
| 2776 | serrno = EVUTIL_SOCKET_ERROR(); |
| 2777 | EVUTIL_CLOSESOCKET(fd); |
| 2778 | EVUTIL_SET_SOCKET_ERROR(serrno); |
| 2779 | return (-1); |
| 2780 | } |
| 2781 |
| 2782 | static struct addrinfo * |
| 2783 | make_addrinfo(const char *address, u_short port) |
| 2784 | { |
| 2785 | struct addrinfo *aitop = NULL; |
| 2786 |
| 2787 | #ifdef HAVE_GETADDRINFO |
| 2788 | struct addrinfo ai; |
| 2789 | char strport[NI_MAXSERV]; |
| 2790 | int ai_result; |
| 2791 |
| 2792 | memset(&ai, 0, sizeof(ai)); |
| 2793 | ai.ai_family = AF_INET; |
| 2794 | ai.ai_socktype = SOCK_STREAM; |
| 2795 | ai.ai_flags = AI_PASSIVE; /* turn NULL host name into INADDR_ANY */ |
| 2796 | evutil_snprintf(strport, sizeof(strport), "%d", port); |
| 2797 | if ((ai_result = getaddrinfo(address, strport, &ai, &aitop)) != 0) { |
| 2798 | if ( ai_result == EAI_SYSTEM ) |
| 2799 | event_warn("getaddrinfo"); |
| 2800 | else |
| 2801 | event_warnx("getaddrinfo: %s", gai_strerror(ai_result)); |
| 2802 | return (NULL); |
| 2803 | } |
| 2804 | #else |
| 2805 | static int cur; |
| 2806 | static struct addrinfo ai[2]; /* We will be returning the address of some of this memory so it has to last even after this call. */ |
| 2807 | if (++cur == 2) cur = 0; /* allow calling this function twice */ |
| 2808 |
| 2809 | if (fake_getaddrinfo(address, &ai[cur]) < 0) { |
| 2810 | event_warn("fake_getaddrinfo"); |
| 2811 | return (NULL); |
| 2812 | } |
| 2813 | aitop = &ai[cur]; |
| 2814 | ((struct sockaddr_in *) aitop->ai_addr)->sin_port = htons(port); |
| 2815 | #endif |
| 2816 |
| 2817 | return (aitop); |
| 2818 | } |
| 2819 |
| 2820 | static int |
| 2821 | bind_socket(const char *address, u_short port, int reuse) |
| 2822 | { |
| 2823 | int fd; |
| 2824 | struct addrinfo *aitop = NULL; |
| 2825 |
| 2826 | /* just create an unbound socket */ |
| 2827 | if (address == NULL && port == 0) |
| 2828 | return bind_socket_ai(NULL, 0); |
| 2829 |
| 2830 | aitop = make_addrinfo(address, port); |
| 2831 |
| 2832 | if (aitop == NULL) |
| 2833 | return (-1); |
| 2834 |
| 2835 | fd = bind_socket_ai(aitop, reuse); |
| 2836 |
| 2837 | #ifdef HAVE_GETADDRINFO |
| 2838 | freeaddrinfo(aitop); |
| 2839 | #else |
| 2840 | fake_freeaddrinfo(aitop); |
| 2841 | #endif |
| 2842 |
| 2843 | return (fd); |
| 2844 | } |
| 2845 |
| 2846 | static int |
| 2847 | socket_connect(int fd, const char *address, unsigned short port) |
| 2848 | { |
| 2849 | struct addrinfo *ai = make_addrinfo(address, port); |
| 2850 | int res = -1; |
| 2851 |
| 2852 | if (ai == NULL) { |
| 2853 | event_debug(("%s: make_addrinfo: \"%s:%d\"", |
| 2854 | __func__, address, port)); |
| 2855 | return (-1); |
| 2856 | } |
| 2857 |
| 2858 | if (connect(fd, ai->ai_addr, ai->ai_addrlen) == -1) { |
| 2859 | #ifdef WIN32 |
| 2860 | int tmp_error = WSAGetLastError(); |
| 2861 | if (tmp_error != WSAEWOULDBLOCK && tmp_error != WSAEINVAL && |
| 2862 | tmp_error != WSAEINPROGRESS) { |
| 2863 | goto out; |
| 2864 | } |
| 2865 | #else |
| 2866 | if (errno != EINPROGRESS) { |
| 2867 | goto out; |
| 2868 | } |
| 2869 | #endif |
| 2870 | } |
| 2871 |
| 2872 | /* everything is fine */ |
| 2873 | res = 0; |
| 2874 |
| 2875 | out: |
| 2876 | #ifdef HAVE_GETADDRINFO |
| 2877 | freeaddrinfo(ai); |
| 2878 | #else |
| 2879 | fake_freeaddrinfo(ai); |
| 2880 | #endif |
| 2881 |
| 2882 | return (res); |
| 2883 | } |
| 2884 |