something
This commit is contained in:
parent
f2728eaf68
commit
802e974269
69
src/server.c
69
src/server.c
|
@ -9,10 +9,10 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "server.h"
|
#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) {
|
int send_response(int fd, char *header, char *content_type, void *body, int content_length) {
|
||||||
const int max_response_size = 65536;
|
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);
|
memcpy(response + response_length, body, content_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
char resp[] = "HTTP/1.0 200 OK\r\n"
|
// char resp[] = "HTTP/1.0 200 OK\r\n"
|
||||||
"Server: webserver-c\r\n"
|
// "Server: webserver-c\r\n"
|
||||||
"Content-type: text/html\r\n\r\n"
|
// "Content-type: text/html\r\n\r\n"
|
||||||
"<html>hello, world</html>\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);
|
||||||
|
@ -106,30 +108,49 @@ int main() {
|
||||||
|
|
||||||
// check if request is GET request
|
// 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"
|
// const char resp[] = "HTTP/1.0 400 Bad Request\r\n"
|
||||||
"Server: webserver-c\r\n"
|
// "Server: webserver-c\r\n"
|
||||||
"Content-Type: text/html\r\n\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);
|
|
||||||
|
|
||||||
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
|
// write to the socket
|
||||||
int valwrite = write(newsockfd, resp, strlen(resp));
|
// int valwrite = write(newsockfd, resp, strlen(resp));
|
||||||
if (valwrite < 0) {
|
// if (valwrite < 0) {
|
||||||
perror("webserver (write)");
|
// perror("webserver (write)");
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
close(newsockfd);
|
close(newsockfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_file_path(char *uri, char *file_path) {
|
|
||||||
char *question = strchr(route, '?');
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#define PORT 8080
|
#define PORT 8080
|
||||||
#define BUFFER_SIZE 1024
|
#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";
|
static const char BAD_REQUEST_HEAD[] = "HTTP/1.1 400 Bad Request";
|
||||||
const char NOT_FOUND_RESP[] = "HTTP/1.1 404 Not Found\r\n";
|
static const char NOT_FOUND_HEAD[] = "HTTP/1.1 404 Not Found";
|
||||||
const char OK_RESP[] = "HTTP/1.1 200 OK\r\n";
|
static const char OK_HEAD[] = "HTTP/1.1 200 OK";
|
||||||
|
|
Loading…
Reference in New Issue