From 78a0eb6854fa7f5cd95e199d18b63c46034b29b0 Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 23 May 2024 19:31:49 +0700 Subject: [PATCH] completed send response function --- server | Bin 17184 -> 17216 bytes src/server.c | 39 ++++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/server b/server index 6379fb5140342c098f46726eb47527c4976d8958..f64d85acdea4d81ee6adcafe356949c9e9c6991d 100755 GIT binary patch delta 2952 zcmZ8j4Nz3q6@GWw1r|kEU|oKUJTMz%g1amP`O$S3bn8Q9Xe-*LNm@{JB%|X`py`-U zARCOEoy|y&cC5`z)-*GtvIz4QFQy=``*LU z`{tZ;&+k3=yz}0@(IYH=gdNMZMQxH8evL@}fzqE@Ymr8x?OIum7t}`fwOn=SU{-4)>zyGJhSyAb!+lS{;fAcxLoU}51 z8LTq_1%P}yxG0;-lg*DU1!o830&)Op06ww-88l=np-i*6gGyWIZ8_~=Q~RUH~{*_Fw2Ht>iTQSp z$!}?8$LX}iO>%>oKC$GmUkGiDz^4Rm7MA77WJ3jIq0%QTkk zW_#&Y^5)bRIuGsc?AW)9)}(x!ev)#AHl*h6W7xV$vH<@7VS*QVV7H#FdyjInmX~%xBy~NxO$mBmhpVK7 z{?WNu346Cgu9hMVwH@fPrHQ_mwKAm_We;`Vp*OOY_jux=D{28URYHMDB@&1#q5h+Z z2MHz8J*jTJ4|#Y*@S(BMbt8dk|$O#JVTDA`EtJyRQC#uO0EO;2|RCd0aST zLm2M96ArwU6u7SJoYFPw$MZb;$zbAi}NOMQns1Wtwm^H&1L`ByW8%%q1B z{uv|nDJ3)!4$Lck6TCLhjZ!jn)N>F?PkJ*OcH-jOn1QJjy#? zZFt1p!QJl`9B32v>LR|t;Gw4Q6UdpH3ZR#F9$jO(Vf;iMa2pk;Y_+jvK-0B3? z*h`)`sfAq%eRyO7Sbga(f`kbfadi!*!4Zxh<~xf2 zh#G3H?&=~cL@CX~i z{oJBH7-vePYE;Cr4}hrqL{0c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HcR8pdU@6SFqxh0zXLTu*^c&JKzYJz*jtKB#UC7yd@6`#?1 z$AqMIexhm7rYlMpz;I))E3w1xC|zwg)ES;H9<1X2@k=~OHDdNb8yiT$12ICYW{ZiN z=9K95KvFq*ov(QB%mK4-G?(vXH)$!~<9ikTu6_&duQ#Q5doQNY?3m*M^vL}tl@+YD zznIdr1JRK`{YTRRn))khE!Z&VK+MpxWFIc%^@<1g^F%o-5*^bM?bQ=~5h-F*|3-$Z zyy?_eAg>I6h=>0cN^>3e=;3?x@cd81vBkHLrB#Wac@tE98J6hCB-o)TjwO4X-&9yp zh^IUb^WqE+4~hmo*DSnZ!WX%c5RmE$hNE#hUzN=~WpH5xlsxkV&b*Jwc-SyAhj-&ce zEbDtvzQ(U9wU6#zy=L$U$mj}w6YYrG<{|Bf=Z@-;&mb~7vhBSUI|xHiy3TN&x>=aF zf*qRT__OssfOz#L%!}1%vBjODL{oEE;nn?ucZKz7>%XMWR4G#Z1`cMesy7M`R|;1s z*0&i_!z902)qjYs^XjW>_!PfK8^oGYXuL4aV(*NU2)U;-X)b&0J3bq&^R*|Uz6w@z zr2FB%?n6&>AM%wRIn>kNT_)YxmzyPht^1Kj4oXUE>jVBkdB9g$QQ_NlpR~Wb``BZL z`}-vNTXAb~0oHW_Q}<(&&w~zw?%?v0rdx2fO=r?EDh~P`W^0aVc^W44&BRS zS_t{&PyaEJCtNFsjUxyr+QZF^C273DFPq1Hp5_~BWwU^j+OvpdAUN*JVxV< VwQ{6Jm!6~rNS3>GsR{ttTDE0q8M diff --git a/src/server.c b/src/server.c index b3948c5..cd254c3 100644 --- a/src/server.c +++ b/src/server.c @@ -14,12 +14,11 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont const int max_response_size = 65536; char response[max_response_size]; - time_t rawtime; - struct tm *info; - - info = localtime(&rawtime); - - int response_length = sprintf(response, + time_t current = time(NULL); + char date_time[30]; + strftime(date_time, sizeof(date_time), "%a, %d %b %Y %H:%M:%S %Z", localtime(¤t)); + + int header_length = sprintf(response, "%s\n" "Server: %s\n" "Date: %s\n" @@ -27,19 +26,24 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont "Content-Length: %d\n" "Content-Type: %s\n" "\n", - header, SERVER_VERSION, asctime(info), content_length, content_type); + header, SERVER_VERSION, date_time, content_length, content_type); - memcpy(response + response_length, body, content_length); + printf("%s\n", (char *)body); + + //printf("%p\n", (char *)response); + //printf("%p\n", &header_length); + //printf("%p\n", (void *)(response + header_length)); + memcpy(response + header_length, body, content_length); printf("%s\n", response); - write(fd, response, response_length); + int write_res = write(fd, response, header_length + content_length); + + if (write_res < 0) { + perror("webserver (write)"); + return write_res; + } - //if (write < 0) { - // perror("webserver (write)"); - // return write; - //} - - return 0; + return write_res; } int main() { @@ -147,8 +151,9 @@ int main() { // } char body[100]; - int body_size = snprintf(body, 100, "%s\r\n", "hello"); - send_response(newsockfd, "HTTP/1.1 200 OK", "text/html", "deeznuts", sizeof(body_size)); + 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)); } // write to the socket