apportate/src/http.c

92 lines
1.9 KiB
C
Raw Normal View History

2022-11-24 08:18:05 +00:00
#include "headers.h"
const char REQ_HTTP[] =
2023-02-19 20:44:19 +00:00
{
"GET %s HTTP/1.0\r\nHost: %s\r\n\r\n"
};
2022-11-24 08:18:05 +00:00
int reqgen_http(const char *path, const char *fqdn, char **nbuf)
2023-02-19 20:44:19 +00:00
{
int buflen;
2022-11-24 08:18:05 +00:00
2023-02-19 20:44:19 +00:00
buflen = (strlen(REQ_HTTP) + strlen(path) + strlen(fqdn) + 1);
2022-11-24 08:18:05 +00:00
2023-02-19 20:44:19 +00:00
if( !(*nbuf = calloc(buflen, sizeof(char))))
{
return(ERRMEM);
}
2023-02-19 20:44:19 +00:00
sprintf(*nbuf, REQ_HTTP, path, fqdn);
2022-11-24 08:18:05 +00:00
2023-02-19 20:44:19 +00:00
return(0);
}
2022-12-13 17:12:07 +00:00
int resp_parse_http(char *data)
{
char *buf;
if( NULL == (buf = calloc(4, sizeof(char))))
{
goto err;
}
/* there are nine characters between the start of data and the first character of the response code */
data += 9;
memcpy(buf, data, 3);
return(atoi(buf));
err:
return(0);
}
2023-02-19 20:44:19 +00:00
/* http_header_extract - return the value corresponding to a key if it exists in an http response, otherwise null */
/* key - key to extract corresponding value */
/* data - response header from which to extract key's value */
char *http_get_keyval(char *key, char *data)
2022-12-13 17:12:07 +00:00
{
2023-02-19 20:44:19 +00:00
char *buf;
/* data indices */
char *d_ind, *d_ind2;
buf = NULL;
d_ind = d_ind2 = data;
2022-12-13 17:12:07 +00:00
2023-02-19 20:44:19 +00:00
/* we ensure that our key and each key we compare to are lower-case because some */
/* servers will return mixed-case keys and others single-case. by doing this we */
/* can use full key specifiers, which avoids false matches that would occur as a */
/* result of using partial key specifiers e.g "location" instead of "ocation". */
key = buftolower(key);
for(;*data != '\0'; data++)
{
if(NULL != buf)
2022-12-13 17:12:07 +00:00
{
2023-02-19 20:44:19 +00:00
free(buf);
buf = NULL;
for(; '\n' != *(data - 1); data++);
d_ind = d_ind2 = data;
2022-12-13 17:12:07 +00:00
}
2023-02-19 20:44:19 +00:00
for(; *d_ind != ':'; d_ind++);
buf = substr_extract(data, 0, (d_ind - data));
2022-12-13 17:12:07 +00:00
2023-02-19 20:44:19 +00:00
buf = buftolower(buf);
2022-12-13 17:12:07 +00:00
2023-02-19 20:44:19 +00:00
if(!strcmp(key, buf))
{
free(buf);
for(data = d_ind+2; *(1+d_ind) != '\n'; d_ind++);
buf = substr_extract(data, 0, (d_ind - data));
return(buf);
}
}
2022-12-13 17:12:07 +00:00
return(NULL);
}