diff --git a/src/network.cpp b/src/network.cpp index d313caa..65acf59 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -13,27 +13,30 @@ void TestSockets(int sender_s, int receiver_s) { std::cout << "Attempting to send data to receiver" << std::endl; do { buf_len = recv(sender_s, in_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - pipe packet recv size: " << buf_len << std::endl; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; + return; } - if (buf_len == 0) { continue; } - send(receiver_s, in_buf, buf_len, 0); - std::cout << "LOG (info) - pipe packet size: " << buf_len << std::endl; - } while (buf_len > 0); + if (buf_len == 0) { break; } + buf_len = send(receiver_s, in_buf, buf_len, 0); + std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; + } while (buf_len == BUF_SIZE); std::cout << "Sent data to receiver" << std::endl; // Pass response along from server to browser std::cout << "Attempting to send to browser" << std::endl; do { - buf_len = recv(receiver_s, out_buf, BUF_SIZE, 0); + buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); + std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; if (buf_len == -1) { std::cout << "ERROR (info) - recv" << std::endl; - break; + return; } - if (buf_len == 0) { continue; } - send(sender_s, out_buf, buf_len, 0); - std::cout << "LOG (info) - proxy packet size: " << buf_len << std::endl; - } while (buf_len > 0); + if (buf_len == 0) { break; } + buf_len = send(receiver_s, out_buf, buf_len, 0); + std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; + } while (buf_len == BUF_SIZE); std::cout << "Sent to browser" << std::endl; close(sender_s); close(receiver_s); @@ -55,7 +58,7 @@ void PipeSockets(int sender_s, int receiver_s) { if (buf_len == 0) { break; } buf_len = send(receiver_s, in_buf, buf_len, 0); std::cout << "LOG (info) - pipe packet send size: " << buf_len << std::endl; - } while (buf_len < BUF_SIZE); + } while (buf_len == BUF_SIZE); std::cout << "Sent data to receiver" << std::endl; } @@ -70,13 +73,16 @@ void ProxySockets(int sender_s, int receiver_s) { buf_len = recv(sender_s, out_buf, BUF_SIZE, 0); std::cout << "LOG (info) - proxy packet recv size: " << buf_len << std::endl; if (buf_len == -1) { - std::cout << "ERROR (info) - recv" << std::endl; + std::cout << "ERROR (info) - recv -1" << std::endl; return; } - if (buf_len == 0) { break; } + if (buf_len == 0) { + std::cout << "ERROR (info) - recv 0" << std::endl; + return; + } buf_len = send(receiver_s, out_buf, buf_len, 0); std::cout << "LOG (info) - proxy packet send size: " << buf_len << std::endl; - } while (buf_len < BUF_SIZE); + } while (buf_len == BUF_SIZE); std::cout << "Sent to browser" << std::endl; close(sender_s); close(receiver_s); diff --git a/src/web_server.cpp b/src/web_server.cpp index 348766e..10e8365 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -121,46 +121,42 @@ void ClientRequest(int client_s) { // Receive the GET request from the Web browser retcode = recv(client_s, in_buf, BUF_SIZE, 0); + if (retcode == -1) { std::cerr << "ERROR (info) - recv" << std::endl; } // Handle the GET if there is one (see note #3 in the header) - if (retcode != -1) - { - // Parse out the filename from the GET request - strtok(in_buf, " "); - file_name = strtok(NULL, " "); + // Parse out the filename from the GET request + strtok(in_buf, " "); + file_name = strtok(NULL, " "); - // Open the requested file - // - Start at 2nd char to get rid of leading "\" + // Open the requested file + // - Start at 2nd char to get rid of leading "\" #ifdef WIN - fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE); + fh = open(&file_name[1], O_RDONLY | O_BINARY, S_IREAD | S_IWRITE); #endif #ifdef UNIX - fh = open(&file_name[1], O_RDONLY, S_IREAD | S_IWRITE); + fh = open(&file_name[1], O_RDONLY, S_IREAD | S_IWRITE); #endif - // Generate and send the response (404 if could not open the file) - if (fh == -1) - { - printf("File %s not found - sending an HTTP 404 \n", &file_name[1]); - strcpy(out_buf, NOTOK_404); - send(client_s, out_buf, strlen(out_buf), 0); - strcpy(out_buf, MESS_404); - send(client_s, out_buf, strlen(out_buf), 0); - } - else - { - printf("File %s is being sent \n", &file_name[1]); - if (strstr(file_name, ".gif") != NULL) - strcpy(out_buf, OK_IMAGE); - else - strcpy(out_buf, OK_TEXT); - send(client_s, out_buf, strlen(out_buf), 0); - do { - buf_len = read(fh, out_buf, BUF_SIZE); - send(client_s, out_buf, buf_len, 0); - } while (buf_len > 0); - close(fh); - } - } + // Generate and send the response (404 if could not open the file) + if (fh == -1) + { + printf("File %s not found - sending an HTTP 404 \n", &file_name[1]); + strcpy(out_buf, NOTOK_404); + send(client_s, out_buf, strlen(out_buf), 0); + strcpy(out_buf, MESS_404); + send(client_s, out_buf, strlen(out_buf), 0); + } else { + printf("File %s is being sent \n", &file_name[1]); + if (strstr(file_name, ".gif") != NULL) + strcpy(out_buf, OK_IMAGE); + else + strcpy(out_buf, OK_TEXT); + send(client_s, out_buf, strlen(out_buf), 0); + do { + buf_len = read(fh, out_buf, BUF_SIZE); + buf_len = send(client_s, out_buf, buf_len, 0); + } while (buf_len == BUF_SIZE); + close(fh); + } // Close the client socket and end the thread close(client_s); }