added things
This commit is contained in:
parent
d3c75b26fc
commit
f2728eaf68
|
@ -1,3 +1,9 @@
|
||||||
# simple-web-server
|
# simple-web-server
|
||||||
|
|
||||||
Simple HTTP server in C
|
Simple HTTP server in C
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
- https://bruinsslot.jp/post/simple-http-webserver-in-c/
|
||||||
|
- https://medium.com/@nipunweerasiri/a-simple-web-server-written-in-c-cf7445002e6
|
||||||
|
- https://github.com/AmyShackles/Web-Server-in-C/
|
||||||
|
- https://devdocs.io
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
const char src[20] = "HA! poopoohead!";
|
||||||
|
char dest[100];
|
||||||
|
|
||||||
|
int eh = sprintf(dest, "%s", "goofy");
|
||||||
|
|
||||||
|
memcpy(dest + 6, src, strlen(src) + 1);
|
||||||
|
|
||||||
|
printf("Output: %s\n", dest);
|
||||||
|
|
||||||
|
printf("src[0] %p\n", &src[0]);
|
||||||
|
printf("src[0] %p\n\n", src);
|
||||||
|
printf("src[1] %p\n", &src[1]);
|
||||||
|
printf("src[1] %p\n", src + 1);
|
||||||
|
printf("src[50] %p\n", src + 49);
|
||||||
|
|
||||||
|
printf("%ld", sizeof(dest));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void get_file_path(char *uri, char *file_path) {
|
||||||
|
char *question = strchr(uri, '?');
|
||||||
|
}
|
|
@ -6,9 +6,35 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "server.h"
|
||||||
|
#include "file.c"
|
||||||
|
|
||||||
#define PORT 8080
|
#define PORT 8080
|
||||||
#define BUFFER_SIZE 1024
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
int send_response(int fd, char *header, char *content_type, void *body, int content_length) {
|
||||||
|
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,
|
||||||
|
"%s\n"
|
||||||
|
"Date: %s"
|
||||||
|
"Connection: close\n"
|
||||||
|
"Content-Length: %d\n"
|
||||||
|
"Content-Type: %s\n"
|
||||||
|
"\n",
|
||||||
|
header, asctime(info), content_length, content_type);
|
||||||
|
|
||||||
|
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"
|
||||||
|
@ -82,10 +108,13 @@ int main() {
|
||||||
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));
|
write(newsockfd, resp, strlen(resp));
|
||||||
} else {
|
} else {
|
||||||
// TO DO: IMPLEMENT FILE READING
|
char file_path[BUFFER_SIZE];
|
||||||
|
get_file_path(uri, file_path);
|
||||||
|
|
||||||
|
FILE *file = fopen(file_path, "r");
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to the socket
|
// write to the socket
|
||||||
|
@ -100,3 +129,7 @@ int main() {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void get_file_path(char *uri, char *file_path) {
|
||||||
|
char *question = strchr(route, '?');
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
#define PORT 8080
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
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";
|
Loading…
Reference in New Issue