Question: Extend the HTTP _ Server by adding code to the connection _ handler ( ) function that breaks down the HTTP request from the client

Extend the HTTP_Server by adding code to the connection_handler() function that breaks down the HTTP request from the client into method, url, and protocol variables. Write a concatenated response that includes the hello, method, url, and protocol variables.
The output should look something like:
HTTP/1.1200 OK
Content-Type: text/html
Content-Length: 12
Method: %s
URL: %s
Protocol: %s
Hello, world!
End the output with two new lines (
). Your buffer variable, what you read from the client, should be 1024 characters long. Your method variable should be 16 characters long. Your url variable should be 1024 characters long, and your protocol variable should be 16 characters long. Use the sscanf() function to extract information pieces from the buffer variable.
Server.c
#include
#include
#include
#include
#include
#include
#define PORT 8080
void *connection_handler(void *socket_desc){
int sock =*(int*)socket_desc;
char *hello ="HTTP/1.1200 OK
Content-Type: text/html
Content-Length: 12
Hello, world!";
write(sock, hello, strlen(hello));
printf("Response sent
");
close(sock);
free(socket_desc);
return NULL;
}
int main(){
int server_fd, new_socket, *new_sock;
struct sockaddr_in address;
int addrlen = sizeof(address);
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))==0){
perror("socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd,(struct sockaddr *)&address, sizeof(address))<0){
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd,3)<0){
perror("listen");
exit(EXIT_FAILURE);
}
while(1){
if ((new_socket = accept(server_fd,(struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
perror("accept");
exit(EXIT_FAILURE);
}
pthread_t thread;
new_sock = malloc(sizeof(int));
*new_sock = new_socket;
if (pthread_create(&thread, NULL, connection_handler, (void*)new_sock)<0){
perror("could not create thread");
exit(EXIT_FAILURE);
}
// Detach the thread so it doesn't need to be joined
pthread_detach(thread);
}
return 0;
}
Client.c
#include
#include
#include
#include
#include
#include
#define PORT 8080
int main(){
int sock =0, valread;
struct sockaddr_in serv_addr;
char *hello = "GET / HTTP/1.1
Host: localhost
";
char buffer[1024]={0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0))<0){
printf("
Socket creation error
");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
printf("
Invalid address/ Address not supported
");
return -1;
}
if (connect(sock,(struct sockaddr *)&serv_addr, sizeof(serv_addr))<0){
printf("
Connection Failed
");
return -1;
}
send(sock , hello , strlen(hello),0);
printf("GET message sent
");
valread = read( sock , buffer, 1024);
printf("%s
",buffer );
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!