reduice code
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 26 May 2023 13:03:50 +0000 (15:03 +0200)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Fri, 26 May 2023 13:03:50 +0000 (15:03 +0200)
server.c
webserver.c

index f9db2917480e8ad13b2a89796d9e74de08aeefa7..b8c5eed8e4dea030c2bbffec03853f658e9f8ed9 100644 (file)
--- a/server.c
+++ b/server.c
@@ -82,10 +82,20 @@ void init_network_context (void)
 
 /* terminate network context */
 
+void _closesocket (socket_t sock)
+{
+    if (sock != INVALID_SOCKET) {
+        int rc = closesocket (sock);
+        if (rc == SOCKET_ERROR) {
+            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
+        }
+    }
+}
+
 void terminate_network_context (void)
 {
-    closesocket (sock);
-    closesocket (conn);
+    _closesocket (sock);
+    _closesocket (conn);
 #ifdef _WIN32 /* Windows */
     WSACleanup ();
 #endif
@@ -99,13 +109,7 @@ int open_listening_socket (int port)
 
     VERBOSE (DEBUG, PRINT ("Opening socket\n"));
     //socket_t sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
-    if (sock != INVALID_SOCKET) {
-        int rc = closesocket (sock);
-        if (rc == SOCKET_ERROR) {
-            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
-        }
-        sock = INVALID_SOCKET;
-    }
+    _closesocket (sock);
     sock = socket (AF_INET, SOCK_STREAM, 0);
     if (sock == INVALID_SOCKET) {
         return 0;
@@ -123,10 +127,7 @@ int open_listening_socket (int port)
     int rc = bind (sock, (struct sockaddr *)&addr, sizeof (addr));
     if (rc == SOCKET_ERROR) {
         VERBOSE (ERROR, PERROR ("error: bind %d\n", ERRNO));
-        rc = closesocket (sock);
-        if (rc == SOCKET_ERROR) {
-            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
-        }
+        _closesocket (sock);
         sock = INVALID_SOCKET;
         return 0;
     }
@@ -138,10 +139,7 @@ int open_listening_socket (int port)
     rc = setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
     if (rc < 0) {
         VERBOSE (ERROR, PERROR ("error: %s\n", "setsockopt/TCP_NODELAY"));
-        closesocket (sock);
-        if (rc == SOCKET_ERROR) {
-            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
-        }
+        _closesocket (sock);
         sock = INVALID_SOCKET;
         return 0;
     }
@@ -149,10 +147,7 @@ int open_listening_socket (int port)
     rc = listen (sock, BACKLOG);
     if (rc < 0) {
         VERBOSE (ERROR, PERROR ("error: %s\n", "listen"));
-        closesocket (sock);
-        if (rc == SOCKET_ERROR) {
-            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
-        }
+        _closesocket (sock);
         sock = INVALID_SOCKET;
         return 0;
     }
@@ -170,10 +165,7 @@ int accept_incoming_connection (void)
     }
 
     if (conn != INVALID_SOCKET) {
-        int rc = closesocket (conn);
-        if (rc == SOCKET_ERROR) {
-            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
-        }
+        _closesocket (conn);
         conn = INVALID_SOCKET;
     }
     conn = accept (sock, NULL, 0);
@@ -188,10 +180,7 @@ int accept_incoming_connection (void)
     int rc = setsockopt (conn, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof (val));
     if (rc < 0) {
         VERBOSE (ERROR, PERROR ("error: %s\n", "setsockopt/TCP_NODELAY"));
-        closesocket (conn);
-        if (rc == SOCKET_ERROR) {
-            VERBOSE (ERROR, PERROR ("error: close %d\n", ERRNO));
-        }
+        _closesocket (conn);
         conn = INVALID_SOCKET;
         return 0;
     }
@@ -203,10 +192,7 @@ int accept_incoming_connection (void)
 
 void close_connection (void)
 {
-    int rc = closesocket (conn);
-    if (rc == SOCKET_ERROR) {
-        VERBOSE (ERROR, PERROR ("error: %d\n", ERRNO));
-    }
+    _closesocket (conn);
     conn = INVALID_SOCKET;
 }
 
@@ -240,20 +226,12 @@ int receive_data (char **pdata)
         int rc = recv (conn, buffer, BUFFER_SIZE, 0);
         VERBOSE (DEBUG, PRINT ("rc: %d\nerrno: %d\n", rc, ERRNO));
 
-        if (rc == 0) { /* sock closed */
-            if (data) {
-                free (data);
-                data = NULL;
-                len  = 0;
-            }
-            break;
-
-        } else if ((rc < 0) && (ERRNO != EAGAIN)) { /* error */
+        if (rc == 0) { /* sock closed or error */
             if (data) {
                 free (data);
                 data = NULL;
             }
-            len  = -1;
+            len  = (rc < 0) ? -1 : 0;
             break;
 
         } else if (rc > 0) {
@@ -283,11 +261,8 @@ int send_data (char *data, int len)
     while (index < len) {
         int rc = send (conn, data + index, len - index, 0);
 
-        if (rc == 0) { /* sock closed */
-            index = 0;
-            break;
-        } else if ((rc < 0) && (ERRNO != EAGAIN)) { /* error */
-            index = -1;
+        if (rc <= 0) { /* sock closed or error */
+            index = (rc < 0) ? -1 : 0;
             break;
         } else if (rc > 0) {
             index += rc;
index ca44de73ec8ab3ccf9b13d539b93245b7893a607..770d75dd5d68e5680f827ecfa918bdd65ef520df 100644 (file)
@@ -208,9 +208,9 @@ int main (int argc, char *argv[])
 // test: webserver.exe -r 2>&1 | grep -q 'missing directory name'
 // test: webserver.exe >& test.log & pid=$!; sleep 1; kill -QUIT $pid; grep -q 'Listening socket on port 8080' test.log
 // test: webserver.exe -p 8000 >& test.log & pid=$!; sleep 1; kill -ABRT $pid; grep -q 'Listening socket on port 8000' test.log
-// test: webserver.exe -c iso-8859-1 -r webroot -s localhost >&/dev/null & pid=$!; sleep 1; curl http://localhost:8080/index.html -v > test; kill -TERM $pid; grep -q '<title>Test</title>' test
-// test: webserver.exe -v 3 -p 8001 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8001/index.html -v > test; kill -TERM $pid
-// test: webserver.exe -c iso-8859-1 -r webroot -s localhost >&/dev/null & pid=$!; sleep 1; curl http://localhost:8080/not_found.html -v > test; kill -TERM $pid; grep -q '404' test
+// test: webserver.exe -c iso-8859-1 -r webroot -s localhost >&/dev/null & pid=$!; sleep 1; curl http://localhost:8080/index.html > test; kill -TERM $pid; grep -q '<title>Test</title>' test
+// test: webserver.exe -v 3 -p 8001 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8001/index.html > test; kill -TERM $pid
+// test: webserver.exe -v 3 -p 8002 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8002/not_found.html > test; kill -TERM $pid; grep -q '404' test
 
 
 /* vim: set ts=4 sw=4 et: */