From 7c61ca2038c089a9c16ef20bbf0fcb3209ac7df6 Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 23 May 2024 19:53:18 +0700 Subject: [PATCH] cleaned code --- server | Bin 17216 -> 17248 bytes src/server.c | 81 ++++++++++----------------------------------------- src/server.h | 1 + 3 files changed, 16 insertions(+), 66 deletions(-) diff --git a/server b/server index f64d85acdea4d81ee6adcafe356949c9e9c6991d..35a6a6bdebb0039de5cbec899eea44b2f4194285 100755 GIT binary patch delta 1627 zcmZ8hZA@EL7(VBgufm`dD0i2^Xp06HGfF$YCN`yD-9?fy4U5q@1zm_PF>%HpCNeif zh+Vl6a-tHqAE-a{&$L*H%&e?95GljrdXe)xj_1p#?ITjX%Q`X6eW!05 zM&!h;Na(C3^ppBbQq?9$j15CyS?4)p11H9aP1}uO- zudDq_$wETD3axYGq69NdVZ4|1b1YS?I7d0{AnG862Nq|M1G!<~=;kH1r=;9=0QGvq zgFpOZ7+ynvj&+q(pPT_UaEsVp-w7570WS0%z)hi6Q>e)lS^+l_UxJ+`Xh?thfAmI9 z*ZxDdm~@{>k8RO0gR3}nsoGmt;PqqRga@av9f)&_iP+7K%D7}R!i$#-!$6DWrq;8_ ziilijC2)Y$1vuTq4my;q)jZHr?0Lto<}RS&!6@uNoZCCvS9sDSphdsXo=jnn62Ah+ z*nV7NA3Iz%ADAZ>Vv+bPtUk1cwD4fX_L`a zq534iFS2nfy#TnF`rSBfUZVaD#8u7F4ChURXF}eDUeE9Sh!{k?1=XgHioL@nQu8a} z(5DS@;lcY6PiqA=jb}d)5nym2K~=5(AyfY6Tr}{DxCR@ak%!kc5?WXLXZd?} zg2LbE zU_s%K)Rv!RDWN;Ea|n<3@i>%s^RIU+u2>L{d8^*cMf*OM*xMu}OJi$$YclpnT}Z?# zHXXSLPe~C;rg0^WC)0Q;jTf?@x6*o5pe1PjxUahXH*u0l(w`=!-Lmkg;NFf)xCA8% z(F5D@oovA;(;8trgnf$5WXiOh1^3DH6^F7(S;3p&b*1^ zKF@pJbI!e;?WN&f+P9f{`U`Ga8xx2ajR`E37F!C<#V7&{!cFN`8}uL>Ls2b|iAL=r_BI2#PX0*3?NUh1^3hD@tB0mkF|M)$B!zpvajNCdM zjcDI3aq&U#@j;TTK9045c|D1kiS{w;N~>_~l9fN?o_UX*kSh&KyqAk6mc zI?d@im_&$Hj0#BMfrtZ}0HHU4qjod1q_0oTN2R@r@0Wjd-K}eltUNux`wTGeJ(B!> z23R0ubD_@x#)KG(0nadmu7T?h&BIQR6l88&MVB~z|NrRC20hiFvt>HE{~42JSsnNZ zUV9!K#W#vaZ-`s$1sdj+7axOU7Q&H9U2N+T0DdUQ@{3$(g)xEDIGmQuiXF~1H#lvY zZFgjCDhI0g0wsp8rtgs`9o8%+PjNTR(#>VbUpdY&?OFK zz{vg)1Hw6~fq!(pN%aM8bEgLGh-L0Jq+a2Cb%dv&nVkT~_;uCHz{SSDCE5&mK%VGs z6BCCUX|ieyz%{Ayig30%IP{$bqT+iR>C4@7uG~7nF>U-l4{yPvH^l9uW+yBttAZV= zz!R;J|SWo~S<61lng#S(Y1+f`Ot>e}%Fg!o{e z<<{{jKXWtRgp$y4cIPCA5<%x%qUI z4d+T#L&){;H>+-KO0TKjw#HmdlM)iidO%z=42=%{;aQIFGb0|Pc^_F>xM+*XBLx?W zisNz?a*%|PW;k*Yo|06O68$_mS)DuEGA+<_vY9Dm`Sdq7Tqe2a1S5^aK4?(^vM6$$ zM5~Pb$^Oz z3b^4_+yG~p?8GCj)ciu!{Z`nk!u~4kZDA>Uy`dic zu&-(%fu3ePwI$ZrhI%A6R{Jc}sHemdy8(slDNmK9CD)K>2_E%4XR;0PtB%3{O48X6 Z+n%rtRSJN38Lh9fhello, world\r\n"; // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -59,7 +48,7 @@ int main() { perror("webserver (socket)"); return 1; } - printf("socket created successfully\n"); + printf("[Winnie-HTTP]: Socket created successfully\n"); // create the address to bind the socket to struct sockaddr_in host_addr; @@ -78,33 +67,33 @@ int main() { perror("webserver (bind)"); return 1; } - printf("socket successfully bound to address\n"); + printf("[Winnie-HTTP]: Socket successfully bound to address\n"); // listen to incoming connections if (listen(sockfd, SOMAXCONN) != 0) { perror("webserver (listen)"); return 1; } - printf("server listening for connections\n"); + printf("[Winnie-HTTP]: server listening for connections\n"); for (;;) { // accept incoming connections - int newsockfd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen); - if (newsockfd < 0) { + int client_sock_fd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen); + if (client_sock_fd < 0) { perror("webserver (accept)"); continue; } - printf("connection accepted\n"); + printf("[Winnie-HTTP]: connection accepted\n"); // get client address - int sockn = getsockname(newsockfd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addrlen); + int sockn = getsockname(client_sock_fd, (struct sockaddr *) &client_addr, (socklen_t *) &client_addrlen); if(sockn < 0) { perror("webserver (getsockname)"); continue; } // read from the socket - int valread = read(newsockfd, buffer, BUFFER_SIZE); + int valread = read(client_sock_fd, buffer, BUFFER_SIZE); if (valread < 0) { perror("webserver (read)"); continue; @@ -114,56 +103,16 @@ int main() { char method[BUFFER_SIZE], uri[BUFFER_SIZE], version[BUFFER_SIZE]; sscanf(buffer, "%s %s %s", method, uri, version); printf("[%s:%u] %s %s %s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), method, version, uri); - - // check if request is GET request + if (strncmp("GET", method, 7) != 0) { -// const char resp[] = "HTTP/1.0 400 Bad Request\r\n" -// "Server: webserver-c\r\n" -// "Content-Type: text/html\r\n\r\n"; - - char resp[100]; - int resp_size = snprintf(resp, 100, - "%s\r\n" - "Server: %s\r\n" - "Content-Type: %s\r\n" - "\r\n", - BAD_REQUEST_HEAD, SERVER_VERSION, "text/html"); - - int valwrite = write(newsockfd, resp, resp_size); - if (valwrite < 0) { - perror("webserver (write)"); - continue; - } + send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0); } else { - // char resp[100]; - // int resp_size = snprintf(resp, 100, - // "%s\r\n" - // "Server: %s\r\n" - // "Content-Type: %s\r\n" - // "\r\n", - // OK_HEAD, SERVER_VERSION, "text/html"); - // - // int valwrite = write(newsockfd, resp, resp_size); - // printf("%s\n", OK_HEAD); - // if (valwrite < 0) { - // perror("webserver (write)"); - // continue; - // } - char body[100]; int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh"); - printf("%d\n", body_size); - send_response(newsockfd, "HTTP/1.1 200 OK", "text/html", body, (body_size+1)); + send_response(client_sock_fd, OK_HEAD, "text/html", body, (body_size + 1)); } - // write to the socket - // int valwrite = write(newsockfd, resp, strlen(resp)); - // if (valwrite < 0) { - // perror("webserver (write)"); - // continue; - // } - - close(newsockfd); + close(client_sock_fd); } return 0; diff --git a/src/server.h b/src/server.h index 7e9951b..5ef675f 100644 --- a/src/server.h +++ b/src/server.h @@ -1,5 +1,6 @@ #define PORT 8080 #define BUFFER_SIZE 1024 +#define MAX_RESPONSE_SIZE 65536 static const char SERVER_VERSION[] = "winnie-http-server";