Lines Matching refs:http

2  * "$Id: http.c 148 2006-04-25 16:54:17Z njacobs $"
22 * WWW: http://www.cups.org
82 #include "http-private.h"
92 #include "http.h"
117 static int http_send(http_t *http, http_state_t request,
119 static int http_wait(http_t *http, int msec);
121 static int http_upgrade(http_t *http);
122 static int http_setup_ssl(http_t *http);
123 static void http_shutdown_ssl(http_t *http);
124 static int http_read_ssl(http_t *http, char *buf, int len);
125 static int http_write_ssl(http_t *http, const char *buf, int len);
294 httpCheck(http_t *http) /* I - HTTP connection */
296 return (httpWait(http, 0));
305 httpClearCookie(http_t *http) /* I - Connection */
307 if (!http)
310 if (http->cookie)
312 free(http->cookie);
313 http->cookie = NULL;
323 httpClose(http_t *http) /* I - Connection to close */
325 DEBUG_printf(("httpClose(http=%p)\n", http));
327 if (!http)
330 if (http->input_set)
331 free(http->input_set);
333 if (http->cookie)
334 free(http->cookie);
337 if (http->tls)
338 http_shutdown_ssl(http);
342 closesocket(http->fd);
344 close(http->fd);
347 free(http);
386 http_t *http; /* New HTTP connection */
433 http = calloc(sizeof(http_t), 1);
434 if (http == NULL)
437 http->version = HTTP_1_1;
438 http->blocking = 1;
439 http->activity = time(NULL);
440 http->fd = -1;
447 http->encryption = HTTP_ENCRYPT_ALWAYS;
449 http->encryption = encrypt;
455 strlcpy(http->hostname, host, sizeof(http->hostname));
463 httpAddrLoad(hostaddr, port, i, &(http->hostaddr));
469 if (!httpReconnect(http))
470 return (http);
477 free(http);
487 httpEncryption(http_t *http, /* I - HTTP data */
490 DEBUG_printf(("httpEncryption(http=%p, e=%d)\n", http, e));
493 if (!http)
496 http->encryption = e;
498 if ((http->encryption == HTTP_ENCRYPT_ALWAYS && !http->tls) ||
499 (http->encryption == HTTP_ENCRYPT_NEVER && http->tls))
500 return (httpReconnect(http));
501 else if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls)
502 return (http_upgrade(http));
519 httpReconnect(http_t *http) /* I - HTTP data */
525 DEBUG_printf(("httpReconnect(http=%p)\n", http));
527 if (!http)
531 if (http->tls)
532 http_shutdown_ssl(http);
539 if (http->fd >= 0)
541 closesocket(http->fd);
543 close(http->fd);
550 if ((http->fd = socket(http->hostaddr.addr.sa_family, SOCK_STREAM, 0)) < 0)
553 http->error = WSAGetLastError();
555 http->error = errno;
557 http->status = HTTP_ERROR;
562 fcntl(http->fd, F_SETFD, FD_CLOEXEC); /* Close this socket when starting *
567 setsockopt(http->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
571 setsockopt(http->fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
583 setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
585 setsockopt(http->fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
593 if (http->hostaddr.addr.sa_family == AF_INET6)
594 status = connect(http->fd, (struct sockaddr *)&(http->hostaddr),
595 sizeof(http->hostaddr.ipv6));
599 if (http->hostaddr.addr.sa_family == AF_LOCAL)
600 status = connect(http->fd, (struct sockaddr *)&(http->hostaddr),
601 SUN_LEN(&(http->hostaddr.un)));
604 status = connect(http->fd, (struct sockaddr *)&(http->hostaddr),
605 sizeof(http->hostaddr.ipv4));
610 http->error = WSAGetLastError();
612 http->error = errno;
614 http->status = HTTP_ERROR;
617 closesocket(http->fd);
619 close(http->fd);
622 http->fd = -1;
627 http->error = 0;
628 http->status = HTTP_CONTINUE;
631 if (http->encryption == HTTP_ENCRYPT_ALWAYS)
637 if (http_setup_ssl(http) != 0)
640 closesocket(http->fd);
642 close(http->fd);
648 else if (http->encryption == HTTP_ENCRYPT_REQUIRED)
649 return (http_upgrade(http));
661 httpGetSubField(http_t *http, /* I - HTTP data */
671 DEBUG_printf(("httpGetSubField(http=%p, field=%d, name=\"%s\", value=%p)\n",
672 http, field, name, value));
674 if (http == NULL ||
680 for (fptr = http->fields[field]; *fptr;)
784 httpSetField(http_t *http, /* I - HTTP data */
788 if (http == NULL ||
794 strlcpy(http->fields[field], value, HTTP_MAX_VALUE);
803 httpDelete(http_t *http, /* I - HTTP data */
806 return (http_send(http, HTTP_DELETE, uri));
815 httpGet(http_t *http, /* I - HTTP data */
818 return (http_send(http, HTTP_GET, uri));
827 httpHead(http_t *http, /* I - HTTP data */
830 return (http_send(http, HTTP_HEAD, uri));
839 httpOptions(http_t *http, /* I - HTTP data */
842 return (http_send(http, HTTP_OPTIONS, uri));
851 httpPost(http_t *http, /* I - HTTP data */
854 httpGetLength(http);
856 return (http_send(http, HTTP_POST, uri));
865 httpPut(http_t *http, /* I - HTTP data */
868 httpGetLength(http);
870 return (http_send(http, HTTP_PUT, uri));
879 httpTrace(http_t *http, /* I - HTTP data */
882 return (http_send(http, HTTP_TRACE, uri));
891 httpFlush(http_t *http) /* I - HTTP data */
896 DEBUG_printf(("httpFlush(http=%p), state=%d\n", http, http->state));
898 while (httpRead(http, buffer, sizeof(buffer)) > 0);
907 httpRead(http_t *http, /* I - HTTP data */
915 DEBUG_printf(("httpRead(http=%p, buffer=%p, length=%d)\n",
916 http, buffer, length));
918 if (http == NULL || buffer == NULL)
921 http->activity = time(NULL);
926 if (http->data_encoding == HTTP_ENCODE_CHUNKED &&
927 http->data_remaining <= 0)
931 if (httpGets(len, sizeof(len), http) == NULL)
937 http->data_remaining = strtol(len, NULL, 16);
938 if (http->data_remaining < 0)
945 DEBUG_printf(("httpRead: data_remaining=%d\n", http->data_remaining));
947 if (http->data_remaining <= 0)
954 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
955 httpGets(len, sizeof(len), http);
957 if (http->state == HTTP_POST_RECV)
958 http->state ++;
960 http->state = HTTP_WAITING;
966 http->data_encoding = HTTP_ENCODE_LENGTH;
970 else if (length > http->data_remaining)
971 length = http->data_remaining;
973 if (http->used == 0 && length <= 256)
979 if (!http->blocking && !httpWait(http, 1000))
982 if (http->data_remaining > sizeof(http->buffer))
983 bytes = sizeof(http->buffer);
985 bytes = http->data_remaining;
988 if (http->tls)
989 bytes = http_read_ssl(http, http->buffer, bytes);
996 bytes = recv(http->fd, http->buffer, bytes, 0);
1001 httpDumpData(stdout, "httpRead:", http->buffer, bytes);
1006 http->used = bytes;
1010 http->error = WSAGetLastError();
1015 http->error = errno;
1022 http->error = EPIPE;
1027 if (http->used > 0)
1029 if (length > http->used)
1030 length = http->used;
1036 memcpy(buffer, http->buffer, length);
1037 http->used -= length;
1039 if (http->used > 0)
1040 memmove(http->buffer, http->buffer + length, http->used);
1043 else if (http->tls)
1045 if (!http->blocking && !httpWait(http, 1000))
1048 bytes = http_read_ssl(http, buffer, length);
1053 if (!http->blocking && !httpWait(http, 1000))
1058 while ((bytes = recv(http->fd, buffer, length, 0)) < 0)
1068 http->data_remaining -= bytes;
1072 http->error = WSAGetLastError();
1077 http->error = errno;
1082 http->error = EPIPE;
1086 if (http->data_remaining == 0)
1088 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
1089 httpGets(len, sizeof(len), http);
1091 if (http->data_encoding != HTTP_ENCODE_CHUNKED)
1093 if (http->state == HTTP_POST_RECV)
1094 http->state ++;
1096 http->state = HTTP_WAITING;
1109 httpSetCookie(http_t *http, /* I - Connection */
1112 if (!http)
1115 if (http->cookie)
1116 free(http->cookie);
1119 http->cookie = strdup(cookie);
1121 http->cookie = NULL;
1130 httpWait(http_t *http, /* I - HTTP data */
1137 if (http == NULL)
1140 if (http->used)
1147 return (http_wait(http, msec));
1156 httpWrite(http_t *http, /* I - HTTP data */
1164 if (http == NULL || buffer == NULL)
1167 http->activity = time(NULL);
1169 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
1171 if (httpPrintf(http, "%x\r\n", length) < 0)
1181 DEBUG_printf(("httpWrite: changing states from %d", http->state));
1183 if (http->state == HTTP_POST_RECV)
1184 http->state ++;
1185 else if (http->state == HTTP_PUT_RECV)
1186 http->state = HTTP_STATUS;
1188 http->state = HTTP_WAITING;
1189 DEBUG_printf((" to %d\n", http->state));
1191 if (httpPrintf(http, "\r\n") < 0)
1203 if (http->tls)
1204 bytes = http_write_ssl(http, buffer, length);
1207 bytes = send(http->fd, buffer, length, 0);
1218 if (WSAGetLastError() != http->error)
1220 http->error = WSAGetLastError();
1226 else if (errno != http->error && errno != ECONNRESET)
1228 http->error = errno;
1241 if (http->data_encoding == HTTP_ENCODE_LENGTH)
1242 http->data_remaining -= bytes;
1245 if (http->data_encoding == HTTP_ENCODE_CHUNKED)
1246 if (httpPrintf(http, "\r\n") < 0)
1249 if (http->data_remaining == 0 && http->data_encoding == HTTP_ENCODE_LENGTH)
1256 DEBUG_printf(("httpWrite: changing states from %d", http->state));
1258 if (http->state == HTTP_POST_RECV)
1259 http->state ++;
1260 else if (http->state == HTTP_PUT_RECV)
1261 http->state = HTTP_STATUS;
1263 http->state = HTTP_WAITING;
1265 DEBUG_printf((" to %d\n", http->state));
1279 http_t *http) /* I - HTTP data */
1287 DEBUG_printf(("httpGets(line=%p, length=%d, http=%p)\n", line, length, http));
1289 if (http == NULL || line == NULL)
1304 bufptr = http->buffer;
1305 bufend = http->buffer + http->used;
1313 if (bufptr >= bufend && http->used < HTTP_MAX_BUFFER)
1319 if (!http->blocking && !http_wait(http, 1000))
1323 if (http->tls)
1324 bytes = http_read_ssl(http, bufend, HTTP_MAX_BUFFER - http->used);
1327 bytes = recv(http->fd, bufend, HTTP_MAX_BUFFER - http->used, 0);
1341 if (WSAGetLastError() != http->error)
1343 http->error = WSAGetLastError();
1353 else if (errno != http->error)
1355 http->error = errno;
1364 http->error = EPIPE;
1373 http->used += bytes;
1378 while (bufptr >= bufend && http->used < HTTP_MAX_BUFFER);
1380 http->activity = time(NULL);
1387 bufptr = http->buffer;
1410 http->used -= bytes;
1411 if (http->used > 0)
1412 memmove(http->buffer, bufptr, http->used);
1429 httpPrintf(http_t *http, /* I - HTTP data */
1441 DEBUG_printf(("httpPrintf: httpPrintf(http=%p, format=\"%s\", ...)\n", http, format));
1452 if (http->tls)
1453 nbytes = http_write_ssl(http, bufptr, bytes - tbytes);
1456 nbytes = send(http->fd, bufptr, bytes - tbytes, 0);
1468 if (WSAGetLastError() != http->error)
1470 http->error = WSAGetLastError();
1476 else if (errno != http->error)
1478 http->error = errno;
1552 httpUpdate(http_t *http) /* I - HTTP data */
1561 DEBUG_printf(("httpUpdate(http=%p), state=%d\n", http, http->state));
1567 if (http->state == HTTP_WAITING)
1575 while (httpGets(line, sizeof(line), http) != NULL)
1590 if (http->status == HTTP_CONTINUE)
1591 return (http->status);
1593 if (http->status < HTTP_BAD_REQUEST)
1594 http->digest_tries = 0;
1597 if (http->status == HTTP_SWITCHING_PROTOCOLS && !http->tls)
1599 if (http_setup_ssl(http) != 0)
1602 closesocket(http->fd);
1604 close(http->fd);
1614 httpGetLength(http);
1616 switch (http->state)
1622 http->state ++;
1627 http->state = HTTP_WAITING;
1631 return (http->status);
1642 http->version = (http_version_t)(major * 100 + minor);
1643 http->status = (http_status_t)status;
1665 http->expect = (http_status_t)atoi(value);
1673 httpSetCookie(http, value);
1681 httpSetField(http, field, value);
1685 http->status = HTTP_ERROR;
1694 if (http->error == EPIPE && http->status > HTTP_CONTINUE)
1695 return (http->status);
1697 if (http->error)
1699 DEBUG_printf(("httpUpdate: socket error %d - %s\n", http->error,
1700 strerror(http->error)));
1701 http->status = HTTP_ERROR;
1924 httpGetLength(http_t *http) /* I - HTTP data */
1926 DEBUG_printf(("httpGetLength(http=%p), state=%d\n", http, http->state));
1928 if (strcasecmp(http->fields[HTTP_FIELD_TRANSFER_ENCODING], "chunked") == 0)
1932 http->data_encoding = HTTP_ENCODE_CHUNKED;
1933 http->data_remaining = 0;
1937 http->data_encoding = HTTP_ENCODE_LENGTH;
1947 if (http->fields[HTTP_FIELD_CONTENT_LENGTH][0] == '\0')
1948 http->data_remaining = 2147483647;
1950 http->data_remaining = atoi(http->fields[HTTP_FIELD_CONTENT_LENGTH]);
1952 DEBUG_printf(("httpGetLength: content_length=%d\n", http->data_remaining));
1955 return (http->data_remaining);
1982 http_send(http_t *http, /* I - HTTP data */
2009 DEBUG_printf(("http_send(http=%p, request=HTTP_%s, uri=\"%s\")\n",
2010 http, codes[request], uri));
2012 if (http == NULL || uri == NULL)
2038 if (http->status == HTTP_ERROR || http->status >= HTTP_BAD_REQUEST)
2039 httpReconnect(http);
2045 http->state = request;
2047 http->state ++;
2049 http->status = HTTP_CONTINUE;
2052 if (http->encryption == HTTP_ENCRYPT_REQUIRED && !http->tls)
2054 httpSetField(http, HTTP_FIELD_CONNECTION, "Upgrade");
2055 httpSetField(http, HTTP_FIELD_UPGRADE, "TLS/1.0,SSL/2.0,SSL/3.0");
2059 if (httpPrintf(http, "%s %s HTTP/1.1\r\n", codes[request], buf) < 1)
2061 http->status = HTTP_ERROR;
2066 if (http->fields[i][0] != '\0')
2068 DEBUG_printf(("%s: %s\n", http_fields[i], http->fields[i]));
2070 if (httpPrintf(http, "%s: %s\r\n", http_fields[i], http->fields[i]) < 1)
2072 http->status = HTTP_ERROR;
2077 if (httpPrintf(http, "\r\n") < 1)
2079 http->status = HTTP_ERROR;
2083 httpClearFields(http);
2094 http_wait(http_t *http, /* I - HTTP data */
2105 DEBUG_printf(("http_wait(http=%p, msec=%d)\n", http, msec));
2112 if (http->tls)
2115 if (SSL_pending((SSL *)(http->tls)))
2118 if (gnutls_record_check_pending(((http_tls_t *)(http->tls))->session))
2123 if (!SSLGetBufferedReadSize((SSLContextRef)http->tls, &bytes) && bytes > 0)
2133 if (!http->input_set)
2142 http->input_set = calloc(1, sizeof(fd_set));
2155 http->input_set = calloc(1, set_size);
2158 if (!http->input_set)
2164 FD_SET(http->fd, http->input_set);
2171 nfds = select(http->fd + 1, http->input_set, NULL, NULL, &timeout);
2174 nfds = select(http->fd + 1, http->input_set, NULL, NULL, NULL);
2182 FD_CLR(http->fd, http->input_set);
2194 http_upgrade(http_t *http) /* I - HTTP data */
2200 DEBUG_printf(("http_upgrade(%p)\n", http));
2207 memcpy(&myhttp, http, sizeof(myhttp));
2233 http->fd = myhttp.fd;
2234 http->error = myhttp.error;
2235 http->activity = myhttp.activity;
2236 http->status = myhttp.status;
2237 http->version = myhttp.version;
2238 http->keep_alive = myhttp.keep_alive;
2239 http->used = myhttp.used;
2241 if (http->used)
2242 memcpy(http->buffer, myhttp.buffer, http->used);
2244 http->auth_type = myhttp.auth_type;
2245 http->nonce_count = myhttp.nonce_count;
2247 memcpy(http->nonce, myhttp.nonce, sizeof(http->nonce));
2249 http->tls = myhttp.tls;
2250 http->encryption = myhttp.encryption;
2256 if (!http->tls)
2265 closesocket(http->fd);
2267 close(http->fd);
2270 http->fd = -1;
2284 http_setup_ssl(http_t *http) /* I - HTTP data */
2299 DEBUG_printf(("http_setup_ssl(http=%p)\n", http));
2308 SSL_set_fd(conn, http->fd);
2322 http->error = WSAGetLastError();
2324 http->error = errno;
2326 http->status = HTTP_ERROR;
2336 http->error = errno;
2337 http->status = HTTP_ERROR;
2348 http->error = errno;
2349 http->status = HTTP_ERROR;
2359 gnutls_transport_set_ptr(conn->session, http->fd);
2363 http->error = errno;
2364 http->status = HTTP_ERROR;
2378 error = SSLSetConnection(conn, (SSLConnectionRef)http->fd);
2391 http->error = error;
2392 http->status = HTTP_ERROR;
2396 close(http->fd);
2402 http->tls = conn;
2412 http_shutdown_ssl(http_t *http) /* I - HTTP data */
2419 conn = (SSL *)(http->tls);
2432 conn = (http_tls_t *)(http->tls);
2442 SSLClose((SSLContextRef)http->tls);
2443 SSLDisposeContext((SSLContextRef)http->tls);
2446 http->tls = NULL;
2455 http_read_ssl(http_t *http, /* I - HTTP data */
2460 return (SSL_read((SSL *)(http->tls), buf, len));
2463 return (gnutls_record_recv(((http_tls_t *)(http->tls))->session, buf, len));
2470 error = SSLRead((SSLContextRef)http->tls, buf, len, &processed);
2476 http->error = error;
2489 http_write_ssl(http_t *http, /* I - HTTP data */
2494 return (SSL_write((SSL *)(http->tls), buf, len));
2497 return (gnutls_record_send(((http_tls_t *)(http->tls))->session, buf, len));
2503 error = SSLWrite((SSLContextRef)http->tls, buf, len, &processed);
2509 http->error = error;
2568 * End of "$Id: http.c 148 2006-04-25 16:54:17Z njacobs $"