2323#include <stdio.h>
2424#include <stdlib.h>
2525#include <ctype.h>
26+ #include <errno.h>
2627
2728#include <net/net_http.h>
2829#include <net/net_compat.h>
@@ -133,6 +134,54 @@ struct http_connection_t
133134 bool ssl ;
134135};
135136
137+ static void net_http_log_transport_state (
138+ const struct http_t * state , const char * stage , ssize_t io_len )
139+ {
140+ #if defined(DEBUG )
141+ const char * method = "GET" ;
142+ const char * domain = "<null>" ;
143+ const char * path = "<null>" ;
144+ int port = 0 ;
145+ int fd = -1 ;
146+ int connected = 0 ;
147+
148+ if (state )
149+ {
150+ method = state -> request .method ? state -> request .method : "GET" ;
151+ domain = state -> request .domain ? state -> request .domain : "<null>" ;
152+ path = state -> request .path ? state -> request .path : "<null>" ;
153+ port = state -> request .port ;
154+
155+ if (state -> conn )
156+ {
157+ fd = state -> conn -> fd ;
158+ connected = state -> conn -> connected ? 1 : 0 ;
159+ }
160+ }
161+
162+ fprintf (stderr ,
163+ "[net_http] %s: method=%s host=%s port=%d path=/%s ssl=%d fd=%d connected=%d request_sent=%d err=%d io_len=%ld errno=%d (%s)\n" ,
164+ stage ? stage : "unknown" ,
165+ method ,
166+ domain ,
167+ port ,
168+ path ,
169+ state ? (state -> ssl ? 1 : 0 ) : 0 ,
170+ fd ,
171+ connected ,
172+ state ? (state -> request_sent ? 1 : 0 ) : 0 ,
173+ state ? (state -> err ? 1 : 0 ) : 0 ,
174+ (long )io_len ,
175+ errno ,
176+ strerror (errno ));
177+ fflush (stderr );
178+ #else
179+ (void )state ;
180+ (void )stage ;
181+ (void )io_len ;
182+ #endif
183+ }
184+
136185struct dns_cache_entry
137186{
138187 char * domain ;
@@ -920,13 +969,16 @@ static bool net_http_new_socket(struct http_t *state)
920969 int fd ;
921970 if (!entry -> addr )
922971 {
972+ net_http_log_transport_state (state , "dns_lookup_failed" , -1 );
923973 UNLOCK_DNS_CACHE ();
924974 return false;
925975 }
926976 addr = entry -> addr ;
927977 fd = socket (addr -> ai_family , addr -> ai_socktype , addr -> ai_protocol );
928978 if (fd >= 0 )
929979 state -> conn = net_http_conn_pool_add (state -> request .domain , state -> request .port , fd , state -> ssl );
980+ else
981+ net_http_log_transport_state (state , "socket_create_failed" , -1 );
930982 /* still waiting on thread */
931983 UNLOCK_DNS_CACHE ();
932984 return (fd >= 0 );
@@ -969,12 +1021,15 @@ static bool net_http_connect(struct http_t *state)
9691021 if (state -> ssl )
9701022 {
9711023 if (!conn )
1024+ {
1025+ net_http_log_transport_state (state , "connect_missing_dns_or_conn" , -1 );
9721026 return false;
973-
1027+ }
9741028 for (next_addr = addr ; conn -> fd >= 0 ; conn -> fd = socket_next ((void * * )& next_addr ))
9751029 {
9761030 if (!(conn -> ssl_ctx = ssl_socket_init (conn -> fd , state -> request .domain )))
9771031 {
1032+ net_http_log_transport_state (state , "ssl_init_failed" , -1 );
9781033 socket_close (conn -> fd );
9791034 break ;
9801035 }
@@ -992,6 +1047,7 @@ static bool net_http_connect(struct http_t *state)
9921047
9931048 if (ssl_socket_connect (conn -> ssl_ctx , next_addr , timeout , true) < 0 )
9941049 {
1050+ net_http_log_transport_state (state , "ssl_connect_failed" , -1 );
9951051 ssl_socket_close (conn -> ssl_ctx );
9961052 ssl_socket_free (conn -> ssl_ctx );
9971053 conn -> ssl_ctx = NULL ;
@@ -1019,6 +1075,7 @@ static bool net_http_connect(struct http_t *state)
10191075 return true;
10201076 }
10211077
1078+ net_http_log_transport_state (state , "socket_connect_failed" , -1 );
10221079 socket_close (conn -> fd );
10231080 }
10241081 conn -> fd = -1 ; /* already closed */
@@ -1039,14 +1096,20 @@ static void net_http_send_str(
10391096 {
10401097 if (!ssl_socket_send_all_blocking (
10411098 state -> conn -> ssl_ctx , text , text_size , true))
1099+ {
10421100 state -> err = true;
1101+ net_http_log_transport_state (state , "ssl_send_failed" , -1 );
1102+ }
10431103 }
10441104 else
10451105#endif
10461106 {
10471107 if (!socket_send_all_blocking (
10481108 state -> conn -> fd , text , text_size , true))
1109+ {
10491110 state -> err = true;
1111+ net_http_log_transport_state (state , "socket_send_failed" , -1 );
1112+ }
10501113 }
10511114}
10521115
@@ -1097,9 +1160,12 @@ static bool net_http_send_request(struct http_t *state)
10971160 size_t _len , len ;
10981161 char * len_str = NULL ;
10991162
1100- if (!request -> postdata && !string_is_equal (request -> method , "PUT" ))
1163+ if (!request -> postdata &&
1164+ !string_is_equal (request -> method , "PUT" ) &&
1165+ request -> contentlength > 0 )
11011166 {
11021167 state -> err = true;
1168+ net_http_log_transport_state (state , "post_without_payload" , -1 );
11031169 return true;
11041170 }
11051171
@@ -1460,6 +1526,7 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
14601526 {
14611527 if (_len < 0 || state -> err )
14621528 {
1529+ net_http_log_transport_state (state , "receive_header_failed" , _len );
14631530 net_http_conn_pool_remove (state -> conn );
14641531 state -> conn = NULL ;
14651532 state -> err = true;
@@ -1474,6 +1541,7 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total)
14741541 {
14751542 if (!net_http_receive_body (state , _len ))
14761543 {
1544+ net_http_log_transport_state (state , "receive_body_failed" , _len );
14771545 net_http_conn_pool_remove (state -> conn );
14781546 state -> conn = NULL ;
14791547 state -> err = true;
@@ -1548,13 +1616,20 @@ int net_http_status(struct http_t *state)
15481616 * caller of net_http_new; it is not freed by net_http_delete().
15491617 * If the status is not 20x and accept_err is false, it returns NULL.
15501618 **/
1551- struct string_list * net_http_headers (struct http_t * state )
1619+ struct string_list * net_http_headers_ex (struct http_t * state , bool accept_err )
15521620{
1553- if (!state || !state -> err )
1621+ if (!state )
1622+ return NULL ;
1623+ if (!accept_err && !state -> err )
15541624 return NULL ;
15551625 return state -> response .headers ;
15561626}
15571627
1628+ struct string_list * net_http_headers (struct http_t * state )
1629+ {
1630+ return net_http_headers_ex (state , false);
1631+ }
1632+
15581633/**
15591634 * net_http_data:
15601635 *
0 commit comments