Created
November 5, 2022 14:32
-
-
Save yeonwooz/4d669b9f043ea963f1eee85e1e9319b4 to your computer and use it in GitHub Desktop.
tiny 웹서버
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* $begin tinymain */ | |
/* | |
* tiny.c - A simple, iterative HTTP/1.0 Web server that uses the | |
* GET method to serve static and dynamic content. | |
* | |
* Updated 11/2019 droh | |
* - Fixed sprintf() aliasing issue in serve_static(), and clienterror(). | |
*/ | |
#include "csapp.h" | |
void doit(int fd); | |
void read_requesthdrs(rio_t *rp); | |
int parse_uri(char *uri, char *filename, char *cgiargs); | |
void serve_static(int fd, char *filename, int filesize); | |
void get_filetype(char *filename, char *filetype); | |
void serve_dynamic(int fd, char *filename, char *cgiargs); | |
void clienterror(int fd, char *cause, char *errnum, char *shortmsg, | |
char *longmsg); | |
int main(int argc, char **argv) { | |
int listenfd, connfd; | |
char hostname[MAXLINE], port[MAXLINE]; | |
socklen_t clientlen; | |
struct sockaddr_storage clientaddr; | |
/* Check command line args */ | |
if (argc != 2) { | |
fprintf(stderr, "usage: %s <port>\n", argv[0]); | |
exit(1); | |
} | |
listenfd = Open_listenfd(argv[1]); | |
while (1) { | |
clientlen = sizeof(clientaddr); | |
connfd = Accept(listenfd, (SA *)&clientaddr, | |
&clientlen); // line:netp:tiny:accept | |
Getnameinfo((SA *)&clientaddr, clientlen, hostname, MAXLINE, port, MAXLINE, | |
0); | |
printf("Accepted connection from (%s, %s)\n", hostname, port); | |
doit(connfd); // line:netp:tiny:doit | |
Close(connfd); // line:netp:tiny:close | |
} | |
} | |
void doit(int fd) { | |
/* 요청헤더를 읽는다 */ | |
/* GET요청으로부터 URI를 파싱한다 */ | |
if(is_static) | |
/* serve_static(fd, filename, sbuf.st_size); // 정적파일 요청 처리 */ | |
else | |
/* serve_dynamic(fd, filename, cgiargs); // 동적파일 요청 처리 */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment