* Improved printf formats and debug code.
authorUrban Wallasch <urban.wallasch@freenet.de>
Sat, 26 Oct 2019 08:41:02 +0000 (10:41 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Sat, 26 Oct 2019 08:41:02 +0000 (10:41 +0200)
telehttpd.c

index 95363c40133b2662a237208b6fe4a33533b18fa2..a219c8433ae53f9f875d2ba0116500152432863b 100644 (file)
@@ -25,6 +25,7 @@
 
 struct thread_data_t {
     int sock;
+    int id;
 };
 
 struct {
@@ -170,11 +171,11 @@ static int respond( int sock, const char *req ) {
                 "Access-Control-Allow-Origin: %s\r\n"
                 "Content-type: text/json\r\n"
                 "Connection: keep-alive\r\n"
-                "Content-Length: %lu\r\n"
+                "Content-Length: %zu\r\n"
                 "\r\n",
                 host,
                 origin,
-                (unsigned long)jsz
+                jsz
             );
         if ( 0 < n && (size_t)n < sizeof hdr && n == sockwrite( sock, hdr, n ) )
             ret = sockwrite( sock, jbuf, jsz );
@@ -195,10 +196,10 @@ static int respond( int sock, const char *req ) {
                 "Host: %s\r\n"
                 "Content-type: text/html\r\n"
                 "Connection: keep-alive\r\n"
-                "Content-Length: %ld\r\n"
+                "Content-Length: %jd\r\n"
                 "\r\n",
                 host,
-                (long)st.st_size
+                (intmax_t)st.st_size
             );
         if ( 0 < n && (size_t)n < sizeof hdr && n == sockwrite( sock, hdr, n ) ) {
             ret = sendfile_tm( sock, fi, st.st_size, 1000 );
@@ -270,7 +271,7 @@ static void *handle_conn( void *p ) {
     struct thread_data_t *td = p;
     char req[2048];
 
-    DPRINT("-- new thread --\n");
+    DPRINT( "-- thread %d started --\n", td->id );
     DPRINT( "sock: %d\n", td->sock );
     while ( 0 == rcv_request(td->sock, req, sizeof req, 5000) ) {
         DPRINT( "request:\n%s", req);
@@ -278,13 +279,13 @@ static void *handle_conn( void *p ) {
             break;
     }
     net_close( td->sock );
+    DPRINT( "-- thread %d terminating --\n", td->id );
     free( td );
-    DPRINT("-- thread terminated --\n");
     return NULL;
 }
 
 static int serve_http(void) {
-    int as, ss;
+    int as, ss, id = 0;
     struct thread_data_t *td;
     pthread_t tid;
     pthread_attr_t attr;
@@ -307,6 +308,7 @@ static int serve_http(void) {
             if ( NULL != (td = malloc( sizeof *td )) ) {
                 int e;
                 td->sock = as;
+                td->id = ++id;
                 pthread_attr_init( &attr );
                 pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
                 if ( 0 != ( e = pthread_create( &tid, &attr, handle_conn, td ) ) ) {