httpc
Loading...
Searching...
No Matches
request_parser.h File Reference
#include "linkedlist.h"
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  request
 

Typedefs

typedef enum methods methods
 
typedef struct request request
 

Enumerations

enum  methods { GET , POST , HEAD }
 

Functions

int parse_route_from_request (char request[], char buffer[], size_t buf_size)
 parse a route from a request
 
int create_request (char request_buffer[], request *req)
 given a request buffer, populate the request struct
 
ll_nodeget_header (char *header_line, char **header_save)
 
int get_headers (char headers_buffer[], request *req)
 
int get_request_line (char **headers_buffer, char request_buffer[], request *req)
 

Function Documentation

◆ create_request()

int create_request ( char request_buffer[],
request * req )

given a request buffer, populate the request struct

Parameters
request_bufferthe buffer to use as a source
reqthe request struct to populate
Returns
-1 if there was an error, 0 if there wasn't

This function is the first step to create a request out of a raw buffer that comes from the socket.

◆ get_header()

ll_node * get_header ( char * header_line,
char ** header_save )
Parameters
header_linea header line in the format "Key: Val"
header_savea save pointer to track the header lines
Returns
an ll_node pointer representing this header

◆ get_headers()

int get_headers ( char headers_buffer[],
request * req )
Parameters
headers_bufferthe entire buffer of headers, deliminated by "\r\n"
reqthe request struct that these headers will be added to
Returns
-1 if there was some error, 0 if no error was encountered

Given the first header line, the headers root, and the line save pointer, add each header to the headers linked list

◆ get_request_line()

int get_request_line ( char ** headers_buffer,
char request_buffer[],
request * req )
Parameters
headers_buffera save pointer that will point to the beginning of the headers.
request_bufferthe buffer containing the entire request
reqthe request struct that will will receive the request line properties

Given an http request line, break out the method & uri, then add it to the request struct. Use headers_buffer as a save pointer. This will make it so it points to the headers, as they come right after the first "\r\n".

◆ parse_route_from_request()

int parse_route_from_request ( char request[],
char buffer[],
size_t buf_size )

parse a route from a request

Parameters
requestthe request line of the HTTP request
bufferthe destination buffer to put the route
buf_sizethe size (in bytes) of the buffer
Returns
number of bytes written. 0 if the route is bigger than the buffer

This function will copy the entire route found in the first line of a request. Beginning at "/" and ending at the first whitespace encountered. It is the responsibility of the consumer to null terminate the buffer.