Skip to content

Commit

Permalink
Update HTTP\exams\2024-02-24\web-server.c
Browse files Browse the repository at this point in the history
  • Loading branch information
imAlessas committed Jul 1, 2024
1 parent 1c120aa commit 3932f4a
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions HTTP/exams/2024-02-24/web-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ struct char_map{




int main() {

const int PORT = 9278;
const char CRLF[] = "\r\n";
const int BUFFER_SIZE = 10 * 1024;


// local variable definition
int s, s_double; // socket
int t; // temporary variable
struct sockaddr_in server_address;
struct sockaddr_in client_address;
char buffer[10 * 1024]; // generic buffer
char buffer[BUFFER_SIZE]; // generic buffer
struct char_map headers[100]; // will contain the parsed headers
char * command_line; // first line of the request
int i; // generic index
Expand Down Expand Up @@ -55,7 +57,7 @@ int main() {

// terminate if error
if(t == -1){
perror("bind() fallita");
perror("bind() failed");
return 1;
}

Expand All @@ -65,15 +67,14 @@ int main() {

// terminate if error
if(t == -1){
perror("listen() fallita");
perror("listen() failed");
return 1;
}


// start server
int address_length = sizeof(struct sockaddr);


while(1) {

// dequeue the backlog and process the request into a new socket
Expand All @@ -83,7 +84,7 @@ int main() {

// terminate if error
if(s_double == -1) {
perror("socket() failed");
perror("accept() failed");
return 1;
}

Expand All @@ -98,7 +99,7 @@ int main() {
// null-terminate
buffer[i - 1] = 0;

if( headers[lines].key == NULL )
if( !headers[lines].key[0] )
break;

// set-up new line
Expand All @@ -110,12 +111,12 @@ int main() {
// endo fo the name, start of the value
if( buffer[i] == ':' && headers[lines].value == NULL) {

// null-terminate
buffer[i] = 0;

// set header value
headers[lines].value = &buffer[i + 1];

// null-terminate
buffer[i] = 0;

}

}
Expand Down Expand Up @@ -144,22 +145,36 @@ int main() {
// display the requested file
FILE * file = fopen(uri + 1, "rw");

for(int i = 0; i < BUFFER_SIZE; i++)
buffer[i] = 0;

if(file == NULL) {

// create response
sprintf(buffer, "HTTP/1.1 404 NOT FOUND\r\n\r\n"
"<html><h1>Could not find %s.<h1></html>", uri);
sprintf(buffer, "HTTP/1.1 404 NOT FOUND\r\nConnection: close\r\n\r\n"
"<html><body>"
"<h1>404 Not Found</h1>"
"<p>Sorry, the file <strong>%s</strong> was not found on this server.</p>"
"</body></html>", uri);

printf("%s\n", buffer);

// write response
write(s_double, buffer, strlen(buffer));
if( write(s_double, buffer, strlen(buffer)) == -1 ) {
perror("write() failed");
return 1;
}

} else {

// create response header
sprintf(buffer, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");

// write response
write(s_double, buffer, strlen(buffer));
if( write(s_double, buffer, strlen(buffer)) == -1 ) {
perror("write() failed");
return 1;
}

char chunk_size[20];

Expand All @@ -172,21 +187,33 @@ int main() {
sprintf(chunk_size, "%x\r\n", strlen(buffer));

// write the first line
write(s_double, chunk_size, strlen(chunk_size));
if( write(s_double, chunk_size, strlen(chunk_size)) == -1 ) {
perror("write() failed");
return 1;
}

// write the chunk
write(s_double, buffer, strlen(buffer));
if( write(s_double, buffer, strlen(buffer)) == -1 ) {
perror("write() failed");
return 1;
}

// end of the chunk
write(s_double, CRLF, strlen(CRLF));
if( write(s_double, CRLF, strlen(CRLF)) == -1 ) {
perror("write() failed");
return 1;
}

}

// last chunk
sprintf(buffer, "0\r\n");

// write last chunk
write(s_double, buffer, strlen(buffer));
if( write(s_double, buffer, strlen(buffer)) == -1 ) {
perror("write() failed");
return 1;
}


}
Expand Down

0 comments on commit 3932f4a

Please sign in to comment.