From ce055c56f6c4591e9a9b44f23088a59e672609cd Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Wed, 15 May 2024 15:59:13 +0700 Subject: [PATCH] read requests --- webserver | Bin 15960 -> 16704 bytes webserver.c | 24 ++++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/webserver b/webserver index 78ed470fd9488c7b94a4ba1d35e4b950667f942c..7a6d89f74ed47d9a83a9b7607ecb887215204cff 100755 GIT binary patch literal 16704 zcmeHOeQ;FO6~9S{#7Ia$zWv~d#DbD#2>}C@y5s|1EJ%dVYO6jryD!NOo85Hxtt3vz z#>Tbm#B@?STE}S}+CQpOXPk`HN@;pC$EQiPT8Vpf+g00Y1}C!ThlD#s}s6f9VBeRlv4uXVu}yWn^(3zh${3%=6@uW@PrDi{Ai- ze@T4_D=zxXSS%V7h7q^?ft^O6XQ$ES4~0dc6t}<{ikOxG<@iOnX(2Ja(i9k3SH$`786tTL%B?Rtk))?`4z#r)nyJ8{Bgi<>L*gIE?jhotRYe7fslO@nk)xRm( zTp6I{HVt+3QKi2!!x&@9Jfj{$MvCCDqfby zmRU6u;7si-_^n>(6X@TAmh|aNCet-4b$n1rpUmm_fRH|%({VsgKcCZaAW#1&r{e&g zel(}!z@7ePPR9W|9n0xBP^UX{Iu6k3tvMYB=5$j|#{oHApVM(5PA|&o_~4SBnbUDV zPM79%9Ej5&UdkRX4#4TtIUNVy^vSHAseQ^LL|65~79r!6sr_ADzW$a&KjzSnIP?P! z{bvsSIfwpZhyIj9f83#e+o3<~&>wK<_c`>4L+^6v_d4`1<>@Q_+?G0Vubw)s5B>gZ zdq?Yv@fEM@BkSIPsAgub1lPZId8!va1S(vhBkS%0L?0>Niek-qYc^cEb1)B=?&X<@ z>V<>o_cBTFUn>1qEkd#MlAb!NAA0{*{m}U`z2t~~;*vEB3OKV&6;P3xQ0I;LczqA9 zL$gTSv|S%scNnvJ>Wno_A6|C^l*!BC#7_p{YB^HAAM}!YfSau!`RQF?#LRZ!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M#DjC4KG%sRizsKrKP#wBM*si- diff --git a/webserver.c b/webserver.c index 9fcd8db..e1d00f4 100644 --- a/webserver.c +++ b/webserver.c @@ -10,7 +10,7 @@ int main() { char buffer[BUFFER_SIZE]; - char resp[] = "HTTP/1.0 200\r\n" + char resp[] = "HTTP/1.0 200 OK\r\n" "Server: webserver-c\r\n" "Content-type: text/html\r\n\r\n" "hello, world\r\n"; @@ -18,7 +18,7 @@ int main() { // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { - perror("webserve (socket)"); + perror("webserver (socket)"); return 1; } printf("socket created successfully\n"); @@ -31,8 +31,12 @@ int main() { host_addr.sin_port = htons(PORT); host_addr.sin_addr.s_addr = htonl(INADDR_ANY); + // create client address + struct sockaddr_in client_addr; + int client_addrlen = sizeof(client_addr); + // Bind the socket to the address - if (bind(sockfd, (struct sockaddr *)&host_addr, host_addrlen) != 0) { + if (bind(sockfd, (struct sockaddr *) &host_addr, host_addrlen) != 0) { perror("webserver (bind)"); return 1; } @@ -47,13 +51,20 @@ int main() { for (;;) { // accept incoming connections - int newsockfd = accept(sockfd, (struct sockaddr *)&host_addr, (socklen_t *)&host_addrlen); + int newsockfd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen); if (newsockfd < 0) { perror("webserver (accept)"); continue; } printf("connection accepted\n"); + // get client address + int sockn = getsockname(newsockfd, (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); if (valread < 0) { @@ -61,6 +72,11 @@ int main() { continue; } + // read the request + 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); + // write to the socket int valwrite = write(newsockfd, resp, strlen(resp)); if (valwrite < 0) {