added forking
This commit is contained in:
@@ -14,18 +14,24 @@ int returnRequest(int socket, char *message, int status, SSL *ssl) {
|
||||
} else {
|
||||
send(socket, message, strlen(message), 0);
|
||||
}
|
||||
free(message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int return200Request(int socket, unsigned char *content, SSL *ssl) {
|
||||
char *message = "";
|
||||
message = calloc(strlen((char *)content) + 128, sizeof(unsigned char));
|
||||
sprintf(message, "HTTP/1.1 200 OK\nContent-Length: %zu\nConnection: close\n\n%s\n",
|
||||
if (content != NULL) {
|
||||
message = calloc(strlen((char *)content) + 128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 200 OK\nContent-Length: %zu\nConnection: close\n\n%s\n",
|
||||
strlen((char *)content), content);
|
||||
} else { // Sending empty response
|
||||
message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 200 OK\nContent-Length: 0\nConnection: close\n\n");
|
||||
}
|
||||
return returnRequest(socket, message, 200, ssl);
|
||||
}
|
||||
|
||||
int return201Request(int socket, unsigned char *content, SSL *ssl) {
|
||||
int return201Request(int socket, char *content, SSL *ssl) {
|
||||
char *message = "";
|
||||
message = calloc(strlen((char *)content) + 128, sizeof(unsigned char));
|
||||
sprintf(message, "HTTP/1.1 201 Created\nContent-Length: %zu\nConnection: close\n\n%s\n",
|
||||
@@ -33,22 +39,38 @@ int return201Request(int socket, unsigned char *content, SSL *ssl) {
|
||||
return returnRequest(socket, message, 201, ssl);
|
||||
}
|
||||
|
||||
int return403Request(int socket, SSL *ssl) {
|
||||
char *message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 403 Forbidden\nContent-Length: 0\nConnection: close\n\n");
|
||||
return returnRequest(socket, message, 403, ssl);
|
||||
}
|
||||
|
||||
int return404Request(int socket, SSL *ssl) {
|
||||
char *message = "HTTP/1.1 404 Not Found\nContent-Length: 12\nConnection: close\n\n404 Request\n";
|
||||
char *message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 404 Not Found\nContent-Length: 12\nConnection: close\n\n404 Request\n\n");
|
||||
return returnRequest(socket, message, 404, ssl);
|
||||
}
|
||||
|
||||
int return400Request(int socket, SSL *ssl) {
|
||||
char *message = "HTTP/1.1 400 HTTP Request Not Valid\nContent-Length: 0\nConnection: close\n\n";
|
||||
char *message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 400 HTTP Request Not Valid\nContent-Length: 0\nConnection: close\n\n");
|
||||
return returnRequest(socket, message, 400, ssl);
|
||||
}
|
||||
|
||||
int return500Request(int socket, SSL *ssl) {
|
||||
char *message = "HTTP/1.1 500 Internal Server Error\nContent-Length: 0\nConnection: close\n\n";
|
||||
char *message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 500 Internal Server Error\nContent-Length: 0\nConnection: close\n\n");
|
||||
return returnRequest(socket, message, 500, ssl);
|
||||
}
|
||||
|
||||
int return501Request(int socket, SSL *ssl) {
|
||||
char *message = "HTTP/1.1 501 Not Implemented\nContent-Length: 0\nConnection: close\n\n";
|
||||
char *message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 501 Not Implemented\nContent-Length: 0\nConnection: close\n\n");
|
||||
return returnRequest(socket, message, 501, ssl);
|
||||
}
|
||||
|
||||
int return505Request(int socket, SSL *ssl) {
|
||||
char *message = calloc(128, sizeof(char));
|
||||
sprintf(message, "HTTP/1.1 505 HTTP Version Not Supported\nContent-Length: 0\nConnection: close\n\n");
|
||||
return returnRequest(socket, message, 505, ssl);
|
||||
}
|
||||
|
||||
20
src/utils.c
20
src/utils.c
@@ -1,5 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NUM_SUPPORTED_VERSIONS 2
|
||||
static char supportedHTTPVersions[NUM_SUPPORTED_VERSIONS][16] = {
|
||||
"HTTP/1.1",
|
||||
"HTTP/1.0"
|
||||
};
|
||||
|
||||
int PrintLog(unsigned char *message) {
|
||||
time_t UTCTime;
|
||||
@@ -11,3 +18,16 @@ int PrintLog(unsigned char *message) {
|
||||
(now->tm_year + 1900), now->tm_hour, now->tm_min, now->tm_sec, message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int checkHTTPVersion(char *version) {
|
||||
int supported = -1; // Default fail state
|
||||
char testVer[16];
|
||||
strcpy(testVer, version);
|
||||
//testVer[strlen(version) - 1] = '\0'; // Removing
|
||||
for (int i = 0; i < NUM_SUPPORTED_VERSIONS; i++) {
|
||||
if (!strcmp(testVer, supportedHTTPVersions[i])) {
|
||||
supported = 0;
|
||||
}
|
||||
}
|
||||
return supported;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user