From 802e974269a6699bc8206937bacce28565e8f19c Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Fri, 17 May 2024 23:12:56 +0700 Subject: [PATCH] something --- server | Bin 0 -> 16912 bytes src/server.c | 69 +++++++++++++++++++++++++++++++++------------------ src/server.h | 8 +++--- 3 files changed, 49 insertions(+), 28 deletions(-) create mode 100755 server diff --git a/server b/server new file mode 100755 index 0000000000000000000000000000000000000000..9c9a369abf99d91b2f638df1217181756ca0046e GIT binary patch literal 16912 zcmeHOeQ;FO6~76B5eW$(3RvY~#R;uh621hKx&ZUmrnCd<(ArY}C}Z1kVn=Pu7!8#wGPVu2)5%!tq#bc~XZ)a}wN#Pq@7#OO+qci% zPTT49pYBceoqK-g{O&#PywCgIyZ1IWZzwM-6I>>VF9_0FW+=q34K1s61;Q^@ib^c! zi_66n=;ujH+5Jkus>*nIzO2yrBuL`Tp-KVy83hZL93e^K6)VRtP!^V=TIs}_OjWIW znS_@pY+$FdBY1-J6?IUf(1+N?NW&9e=s>n?lpvP&LbH~!rdz1#+I$cGB~^DaR; z)i1`6eAN2ihnm7jp0s}5MY32~2E)t(#sfw06VuTVU+euZ#4 z{tMx~MexN%@Hc@Q_<75{01CCgp$JZ=#X|m<6v3w!!4pOBTZ-WC12^#VmKp$s;t2t# zZSbj*jCCg>F}p)_TZu$GA!2sCGb!35v9Rd0M4BwGZ2 zt5%uGWGEQx5W5l)+Y(7T5es#7quKTl9bzlQ#^$EA>&zv-Yn<8=-*TtEbeS-l+BTVC zD`D-RP!esM)m;dH*@GU3AM{NohAs+|wvK2KG+a5}GXsrIWH z`Rhxp1m?JKbzW0`h6^VSmm0sS5ym+pq|Q%EAqdx(eyOkYOCfPO2D$kCsz&}7G6)&) zD}``uP^Yx`Rh97R4l2a0F1*TxZ+GFmkC3_3h3idAu;_N-GbN%HJuZBf3-5E`vt4+p zTFO8v1Ema_)sh0W|NvBJFMo7Jt^eWO%p98S)2GZ%$o|%yPL7xyE`=HB0`K*7Z zY4GHi1A~7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7Z9*mk{JDy0I!Bh{P z&UST2Ej%>#)fM)jnE??qm`DVBO*~mm^oouIrVW_kR99CoC@#%}r=91j9+4uVwGHdd z*2bH*HEwM)1C0&q#n#5wn;TnA{I)i2*(|o)s9@9F(Au!6(QMqj-o#XZ^|x(q*wnNR z-C)9v*%;8)!1`8UZfxGNwxQYFvSGv4#x}F9VQq6G7~Z)Lex90uNeFu0f&Go|KZVbbAw&d;fuv>%eNzz@^vTTSP#3`1+}Llm{%4FYp7vuQ!O2D(!Dd7>=eFmZwy^1 zX(yCL-`?aYCTs~S8YF_Qbw_RClac#S_3em5rS}!WN5>tU;&R{eS)KYw>kOk2))ZA8 za*Dxq^bqWdgwTE5rj`*L?V~%VJvyPq0Q2l z>m)ymAKjWWf2HQLG+=WVzW--z1uAa-t(wo0-jBEoU%wcm`#3kB?;}|n?5L%ePxHS% z#F&BO=W!HE?w`j|-uQ#y(-?~RJkDZSqYV@z<(c_B-iZLCRc1bq+gS2A4KA`!8}70b z3XO@F&*LDL^x-KLj-UNl-UAzru~_GE7E3>oQM~ahREQx|D5uQlaURQ2?a2Lm*Z*P7 zU#tCi+{lv0nZ&1@dHLT5Mmgd3o$If@4)b^#F0y#H|9618`G)2>(vxt$yt6#);nN3! zR9JGn3eCmO_%ICa{`o$N<$xz(?w8wp#>40PT9){tHLrM@6VGZsZ$DvZcP)pDtiSdW z$50^`=JR-gCA~X!7jOTs!szBtEKuGo_q)lcbKV$!9hK?$>C=WY4&(X9UVqkEropXp z@p;@gUJDR~{gjr|8UFwV+V{AB9!ELz$#wqd_@Tl)-i|*4MlQ_fd4{8l$y #include "server.h" -#include "file.c" -#define PORT 8080 -#define BUFFER_SIZE 1024 +/* + +TODO: CREATE SENDING RESPONSE FUNCTION int send_response(int fd, char *header, char *content_type, void *body, int content_length) { const int max_response_size = 65536; @@ -35,12 +35,14 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont memcpy(response + response_length, body, content_length); } +*/ + int main() { char buffer[BUFFER_SIZE]; - 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"; +// 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"; // create a socket int sockfd = socket(AF_INET, SOCK_STREAM, 0); @@ -106,30 +108,49 @@ int main() { // 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"; - write(newsockfd, resp, strlen(resp)); - } else { - char file_path[BUFFER_SIZE]; - get_file_path(uri, file_path); +// const char resp[] = "HTTP/1.0 400 Bad Request\r\n" +// "Server: webserver-c\r\n" +// "Content-Type: text/html\r\n\r\n"; - FILE *file = fopen(file_path, "r"); + 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; + } + } 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; + } } // write to the socket - int valwrite = write(newsockfd, resp, strlen(resp)); - if (valwrite < 0) { - perror("webserver (write)"); - continue; - } + // int valwrite = write(newsockfd, resp, strlen(resp)); + // if (valwrite < 0) { + // perror("webserver (write)"); + // continue; + // } close(newsockfd); } return 0; } - -void get_file_path(char *uri, char *file_path) { - char *question = strchr(route, '?'); -} diff --git a/src/server.h b/src/server.h index f0ff549..72ed635 100644 --- a/src/server.h +++ b/src/server.h @@ -1,8 +1,8 @@ #define PORT 8080 #define BUFFER_SIZE 1024 -const char SERVER_VERSION[] = "winnie-http-server/0.0.1"; +static const char SERVER_VERSION[] = "winnie-http-server/0.0.1"; -const char BAD_REQUEST_RESP[] = "HTTP/1.1 400 Bad Request\r\n"; -const char NOT_FOUND_RESP[] = "HTTP/1.1 404 Not Found\r\n"; -const char OK_RESP[] = "HTTP/1.1 200 OK\r\n"; +static const char BAD_REQUEST_HEAD[] = "HTTP/1.1 400 Bad Request"; +static const char NOT_FOUND_HEAD[] = "HTTP/1.1 404 Not Found"; +static const char OK_HEAD[] = "HTTP/1.1 200 OK";