mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-20 03:55:25 +00:00
add comments to client handler
This commit is contained in:
@@ -4,7 +4,21 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 1024
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a single client connection.
|
||||||
|
*
|
||||||
|
* @param args A pointer to the file descriptor of the client
|
||||||
|
*
|
||||||
|
* @return NULL
|
||||||
|
*
|
||||||
|
* @details
|
||||||
|
* This function is designed to be run in a separate thread. It will receive
|
||||||
|
* requests from the client until the client disconnects, at which point it will
|
||||||
|
* return. Reading the requests and building responses is handled by
|
||||||
|
* handle_request.
|
||||||
|
*/
|
||||||
void* client_handler(void* args) {
|
void* client_handler(void* args) {
|
||||||
|
// Cast args
|
||||||
int client = *((int*)args);
|
int client = *((int*)args);
|
||||||
char* buffer = (char*)malloc(BUFFER_SIZE);
|
char* buffer = (char*)malloc(BUFFER_SIZE);
|
||||||
|
|
||||||
@@ -13,9 +27,11 @@ void* client_handler(void* args) {
|
|||||||
// Null terminate buffer
|
// Null terminate buffer
|
||||||
buffer[bytes_read] = '\0';
|
buffer[bytes_read] = '\0';
|
||||||
|
|
||||||
|
// Allocate space for response
|
||||||
char* response = (char*)malloc(BUFFER_SIZE);
|
char* response = (char*)malloc(BUFFER_SIZE);
|
||||||
size_t response_length = BUFFER_SIZE;
|
size_t response_length = BUFFER_SIZE;
|
||||||
|
|
||||||
|
// Handle request
|
||||||
handle_request(buffer, bytes_read, response, &response_length);
|
handle_request(buffer, bytes_read, response, &response_length);
|
||||||
|
|
||||||
// Send response
|
// Send response
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
#ifndef CLIENT_HANDLER_H
|
#ifndef CLIENT_HANDLER_H
|
||||||
#define CLIENT_HANDLER_H
|
#define CLIENT_HANDLER_H
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
void* client_handler(void* args);
|
void* client_handler(void* args);
|
||||||
|
|||||||
Reference in New Issue
Block a user