diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/http.c | 84 |
1 files changed, 43 insertions, 41 deletions
diff --git a/common/http.c b/common/http.c index 5b67accc0..755ab1b92 100644 --- a/common/http.c +++ b/common/http.c @@ -150,7 +150,7 @@ static es_cookie_io_functions_t cookie_functions = cookie_close }; -struct cookie_s +struct cookie_s { int fd; /* File descriptor or -1 if already closed. */ gnutls_session_t tls_session; /* TLS session context or NULL if not used. */ @@ -176,7 +176,7 @@ typedef struct header_s *header_t; /* Our handle context. */ -struct http_context_s +struct http_context_s { unsigned int status_code; int sock; @@ -228,14 +228,14 @@ init_sockets (void) if (initialized) return; - if ( WSAStartup( MAKEWORD (REQ_WINSOCK_MINOR, REQ_WINSOCK_MAJOR), &wsdata ) ) + if ( WSAStartup( MAKEWORD (REQ_WINSOCK_MINOR, REQ_WINSOCK_MAJOR), &wsdata ) ) { - log_error ("error initializing socket library: ec=%d\n", + log_error ("error initializing socket library: ec=%d\n", (int)WSAGetLastError () ); return; } - if ( LOBYTE(wsdata.wVersion) != REQ_WINSOCK_MAJOR - || HIBYTE(wsdata.wVersion) != REQ_WINSOCK_MINOR ) + if ( LOBYTE(wsdata.wVersion) != REQ_WINSOCK_MAJOR + || HIBYTE(wsdata.wVersion) != REQ_WINSOCK_MINOR ) { log_error ("socket library version is %x.%x - but %d.%d needed\n", LOBYTE(wsdata.wVersion), HIBYTE(wsdata.wVersion), @@ -260,7 +260,7 @@ static char * make_header_line (const char *prefix, const char *suffix, const void *data, size_t len ) { - static unsigned char bintoasc[] = + static unsigned char bintoasc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; @@ -278,7 +278,7 @@ make_header_line (const char *prefix, const char *suffix, *p++ = bintoasc[(((s[1]<<2)&074)|((s[2]>>6)&03))&077]; *p++ = bintoasc[s[2]&077]; } - if ( len == 2 ) + if ( len == 2 ) { *p++ = bintoasc[(s[0] >> 2) & 077]; *p++ = bintoasc[(((s[0] <<4)&060)|((s[1] >> 4)&017))&077]; @@ -306,7 +306,7 @@ http_register_tls_callback ( gpg_error_t (*cb) (http_t, void *, int) ) tls_callback = (gpg_error_t (*) (http_t, gnutls_session_t, int))cb; #else (void)cb; -#endif +#endif } @@ -315,13 +315,13 @@ http_register_tls_callback ( gpg_error_t (*cb) (http_t, void *, int) ) pointer for completing the the request and to wait for the response. */ gpg_error_t -http_open (http_t *r_hd, http_req_t reqtype, const char *url, +http_open (http_t *r_hd, http_req_t reqtype, const char *url, const char *auth, unsigned int flags, const char *proxy, void *tls_context, struct http_srv *srv, strlist_t headers) { gpg_error_t err; http_t hd; - + *r_hd = NULL; if (!(reqtype == HTTP_REQ_GET || reqtype == HTTP_REQ_POST)) @@ -339,7 +339,7 @@ http_open (http_t *r_hd, http_req_t reqtype, const char *url, err = http_parse_uri (&hd->uri, url); if (!err) err = send_request (hd, auth, proxy, srv, headers); - + if (err) { if (!hd->fp_read && !hd->fp_write && hd->sock != -1) @@ -382,7 +382,7 @@ http_wait_response (http_t hd) gpg_error_t err; /* Make sure that we are in the data. */ - http_start_data (hd); + http_start_data (hd); /* We dup the socket, to cope with the fact that fclose closes the underlying socket. In TLS mode we don't do that because we can't @@ -455,7 +455,7 @@ http_wait_response (http_t hd) be used as an HTTP proxy and any enabled $http_proxy gets ignored. */ gpg_error_t -http_open_document (http_t *r_hd, const char *document, +http_open_document (http_t *r_hd, const char *document, const char *auth, unsigned int flags, const char *proxy, void *tls_context, struct http_srv *srv, strlist_t headers) @@ -609,7 +609,7 @@ do_parse_uri (parsed_uri_t uri, int only_local_part) p++; if (*p == '/') /* There seems to be a hostname. */ - { + { p++; if ((p2 = strchr (p, '/'))) *p2++ = 0; @@ -667,7 +667,7 @@ do_parse_uri (parsed_uri_t uri, int only_local_part) return gpg_error (GPG_ERR_BAD_URI); /* Path includes a Nul. */ p = p2 ? p2 : NULL; - if (!p || !*p) + if (!p || !*p) return 0; /* We don't have a query string. Okay. */ /* Now parse the query string. */ @@ -861,7 +861,7 @@ send_request (http_t hd, const char *auth, if ( (proxy && *proxy) || ( (hd->flags & HTTP_FLAG_TRY_PROXY) - && (http_proxy = getenv (HTTP_PROXY_ENV)) + && (http_proxy = getenv (HTTP_PROXY_ENV)) && *http_proxy )) { parsed_uri_t uri; @@ -908,7 +908,7 @@ send_request (http_t hd, const char *auth, if (hd->sock == -1) { xfree (proxy_authstr); - return (save_errno + return (save_errno ? gpg_error_from_errno (save_errno) : gpg_error (GPG_ERR_NOT_FOUND)); } @@ -948,7 +948,7 @@ send_request (http_t hd, const char *auth, if (auth || hd->uri->auth) { char *myauth; - + if (auth) { myauth = xtrystrdup (auth); @@ -976,12 +976,12 @@ send_request (http_t hd, const char *auth, return gpg_error_from_syserror (); } } - + p = build_rel_path (hd->uri); if (!p) return gpg_error_from_syserror (); - request = xtrymalloc (2 * strlen (server) + request = xtrymalloc (2 * strlen (server) + strlen (p) + (authstr?strlen(authstr):0) + (proxy_authstr?strlen(proxy_authstr):0) @@ -1008,7 +1008,7 @@ send_request (http_t hd, const char *auth, else { char portstr[35]; - + if (port == 80 || (srv && srv->used_server)) *portstr = 0; else @@ -1190,7 +1190,7 @@ my_read_line ( char *p; if (!buffer) /* Must allocate a new buffer. */ - { + { length = 256; buffer = xtrymalloc (length); *addr_of_buffer = buffer; @@ -1207,7 +1207,7 @@ my_read_line ( while ((c = P_ES(getc) (fp)) != EOF) { if (nbytes == length) /* Increase the buffer. */ - { + { if (length > maxlen) /* Limit reached. */ { /* Skip the rest of the line. */ @@ -1312,7 +1312,7 @@ store_header (http_t hd, char *line) while (*p == ' ' || *p == '\t') p++; value = p; - + for (h=hd->headers; h; h = h->next) if ( !strcmp (h->name, line) ) break; @@ -1544,9 +1544,11 @@ connect_server (const char *server, unsigned short port, unsigned int flags, struct http_srv *srv) { int sock = -1; - int srvcount = 0, fakesrv = 0; + int srvcount = 0; + int fakesrv = 0; int hostfound = 0; - int srvindex, connected, chosen=-1; + int srvindex, connected; + int chosen = -1; int last_errno = 0; struct srventry *serverlist = NULL; @@ -1565,9 +1567,9 @@ connect_server (const char *server, unsigned short port, if ( inaddr != INADDR_NONE ) { struct sockaddr_in addr; - + memset(&addr,0,sizeof(addr)); - + sock = socket(AF_INET,SOCK_STREAM,0); if ( sock==INVALID_SOCKET ) { @@ -1575,9 +1577,9 @@ connect_server (const char *server, unsigned short port, return -1; } - addr.sin_family = AF_INET; + addr.sin_family = AF_INET; addr.sin_port = htons(port); - memcpy (&addr.sin_addr,&inaddr,sizeof(inaddr)); + memcpy (&addr.sin_addr,&inaddr,sizeof(inaddr)); if (!connect (sock,(struct sockaddr *)&addr,sizeof(addr)) ) return sock; @@ -1644,7 +1646,7 @@ connect_server (const char *server, unsigned short port, errno = save_errno; return -1; } - + if (connect (sock, ai->ai_addr, ai->ai_addrlen)) last_errno = errno; else @@ -1680,7 +1682,7 @@ connect_server (const char *server, unsigned short port, xfree (serverlist); return -1; } - + addr.sin_family = host->h_addrtype; if (addr.sin_family != AF_INET) { @@ -1714,7 +1716,7 @@ connect_server (const char *server, unsigned short port, } #endif /* !HAVE_GETADDRINFO */ - if(!fakesrv && chosen>-1 && srv) + if(!fakesrv && chosen >- 1 && srv) { srv->used_server = xstrdup (serverlist[chosen].target); srv->used_port = serverlist[chosen].port; @@ -1753,9 +1755,9 @@ write_server (int sock, const char *data, size_t length) { #ifdef HAVE_W32_SYSTEM int nwritten; - + nwritten = send (sock, data, nleft, 0); - if ( nwritten == SOCKET_ERROR ) + if ( nwritten == SOCKET_ERROR ) { log_info ("network write failed: ec=%d\n", (int)WSAGetLastError ()); return gpg_error (GPG_ERR_NETWORK); @@ -1808,7 +1810,7 @@ cookie_read (void *cookie, void *buffer, size_t size) if (nread == GNUTLS_E_AGAIN) { struct timeval tv; - + tv.tv_sec = 0; tv.tv_usec = 50000; select (0, NULL, NULL, NULL, &tv); @@ -1829,7 +1831,7 @@ cookie_read (void *cookie, void *buffer, size_t size) #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use recv for a socket. */ nread = recv (c->fd, buffer, size, 0); -#else +#else nread = read (c->fd, buffer, size); #endif } @@ -1852,7 +1854,7 @@ cookie_write (void *cookie, const void *buffer, size_t size) int nleft = size; while (nleft > 0) { - nwritten = gnutls_record_send (c->tls_session, buffer, nleft); + nwritten = gnutls_record_send (c->tls_session, buffer, nleft); if (nwritten <= 0) { if (nwritten == GNUTLS_E_INTERRUPTED) @@ -1860,7 +1862,7 @@ cookie_write (void *cookie, const void *buffer, size_t size) if (nwritten == GNUTLS_E_AGAIN) { struct timeval tv; - + tv.tv_sec = 0; tv.tv_usec = 50000; select (0, NULL, NULL, NULL, &tv); @@ -2037,7 +2039,7 @@ main (int argc, char **argv) http_release_parsed_uri (uri); uri = NULL; - rc = http_open_document (&hd, *argv, NULL, + rc = http_open_document (&hd, *argv, NULL, HTTP_FLAG_NO_SHUTDOWN | HTTP_FLAG_NEED_HEADER, NULL, tls_session); if (rc) |