cleaned code
This commit is contained in:
parent
78a0eb6854
commit
7c61ca2038
77
src/server.c
77
src/server.c
|
@ -10,8 +10,8 @@
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
int send_response(int fd, char *header, char *content_type, void *body, int content_length) {
|
int send_response(int fd, const char *header, char *content_type, void *body, int content_length) {
|
||||||
const int max_response_size = 65536;
|
const int max_response_size = MAX_RESPONSE_SIZE;
|
||||||
char response[max_response_size];
|
char response[max_response_size];
|
||||||
|
|
||||||
time_t current = time(NULL);
|
time_t current = time(NULL);
|
||||||
|
@ -28,14 +28,7 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont
|
||||||
"\n",
|
"\n",
|
||||||
header, SERVER_VERSION, date_time, content_length, content_type);
|
header, SERVER_VERSION, date_time, content_length, content_type);
|
||||||
|
|
||||||
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);
|
memcpy(response + header_length, body, content_length);
|
||||||
printf("%s\n", response);
|
|
||||||
|
|
||||||
int write_res = write(fd, response, header_length + content_length);
|
int write_res = write(fd, response, header_length + content_length);
|
||||||
|
|
||||||
if (write_res < 0) {
|
if (write_res < 0) {
|
||||||
|
@ -48,10 +41,6 @@ int send_response(int fd, char *header, char *content_type, void *body, int cont
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char buffer[BUFFER_SIZE];
|
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"
|
|
||||||
// "<html>hello, world</html>\r\n";
|
|
||||||
|
|
||||||
// create a socket
|
// create a socket
|
||||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -59,7 +48,7 @@ int main() {
|
||||||
perror("webserver (socket)");
|
perror("webserver (socket)");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printf("socket created successfully\n");
|
printf("[Winnie-HTTP]: Socket created successfully\n");
|
||||||
|
|
||||||
// create the address to bind the socket to
|
// create the address to bind the socket to
|
||||||
struct sockaddr_in host_addr;
|
struct sockaddr_in host_addr;
|
||||||
|
@ -78,33 +67,33 @@ int main() {
|
||||||
perror("webserver (bind)");
|
perror("webserver (bind)");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printf("socket successfully bound to address\n");
|
printf("[Winnie-HTTP]: Socket successfully bound to address\n");
|
||||||
|
|
||||||
// listen to incoming connections
|
// listen to incoming connections
|
||||||
if (listen(sockfd, SOMAXCONN) != 0) {
|
if (listen(sockfd, SOMAXCONN) != 0) {
|
||||||
perror("webserver (listen)");
|
perror("webserver (listen)");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printf("server listening for connections\n");
|
printf("[Winnie-HTTP]: server listening for connections\n");
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// accept incoming connections
|
// accept incoming connections
|
||||||
int newsockfd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen);
|
int client_sock_fd = accept(sockfd, (struct sockaddr *) &host_addr, (socklen_t *) &host_addrlen);
|
||||||
if (newsockfd < 0) {
|
if (client_sock_fd < 0) {
|
||||||
perror("webserver (accept)");
|
perror("webserver (accept)");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
printf("connection accepted\n");
|
printf("[Winnie-HTTP]: connection accepted\n");
|
||||||
|
|
||||||
// get client address
|
// 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) {
|
if(sockn < 0) {
|
||||||
perror("webserver (getsockname)");
|
perror("webserver (getsockname)");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read from the socket
|
// read from the socket
|
||||||
int valread = read(newsockfd, buffer, BUFFER_SIZE);
|
int valread = read(client_sock_fd, buffer, BUFFER_SIZE);
|
||||||
if (valread < 0) {
|
if (valread < 0) {
|
||||||
perror("webserver (read)");
|
perror("webserver (read)");
|
||||||
continue;
|
continue;
|
||||||
|
@ -115,55 +104,15 @@ int main() {
|
||||||
sscanf(buffer, "%s %s %s", method, uri, version);
|
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);
|
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) {
|
if (strncmp("GET", method, 7) != 0) {
|
||||||
// const char resp[] = "HTTP/1.0 400 Bad Request\r\n"
|
send_response(client_sock_fd, BAD_REQUEST_HEAD, "text/html", NULL, 0);
|
||||||
// "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;
|
|
||||||
}
|
|
||||||
} else {
|
} 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];
|
char body[100];
|
||||||
int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
|
int body_size = snprintf(body, 100, "%s\r\n", "hello what up bruh");
|
||||||
printf("%d\n", body_size);
|
send_response(client_sock_fd, OK_HEAD, "text/html", body, (body_size + 1));
|
||||||
send_response(newsockfd, "HTTP/1.1 200 OK", "text/html", body, (body_size+1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to the socket
|
close(client_sock_fd);
|
||||||
// int valwrite = write(newsockfd, resp, strlen(resp));
|
|
||||||
// if (valwrite < 0) {
|
|
||||||
// perror("webserver (write)");
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
close(newsockfd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#define PORT 8080
|
#define PORT 8080
|
||||||
#define BUFFER_SIZE 1024
|
#define BUFFER_SIZE 1024
|
||||||
|
#define MAX_RESPONSE_SIZE 65536
|
||||||
|
|
||||||
static const char SERVER_VERSION[] = "winnie-http-server";
|
static const char SERVER_VERSION[] = "winnie-http-server";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue