apportate/src/http.c

73 lines
1.2 KiB
C

#include "headers.h"
const char REQ_HTTP[] =
{
"GET %s HTTP/1.0\r\nHost: %s\r\n\r\n"
};
int reqgen_http(const char *path, const char *fqdn, char **nbuf)
{
int buflen;
buflen = (strlen(REQ_HTTP) + strlen(path) + strlen(fqdn) + 1);
if( !(*nbuf = calloc(buflen, sizeof(char))))
{
return(ERRMEM);
}
sprintf(*nbuf, REQ_HTTP, path, fqdn);
return(0);
}
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);
}
char *http_header_extract(char *key, char *data)
{
char *keyp, *keyp_end;
char *returnp;
if( NULL == (keyp = strstr(data, key)))
{
goto err;
}
else
{
for(; 0 != *keyp && *keyp != ':'; keyp++);
for(; 0 != *keyp && !isalnum(*keyp); keyp++);
for(keyp_end = keyp; 0 != *keyp_end && '\r' != *keyp_end; keyp_end++);
if( NULL == (returnp = calloc((int) (keyp_end - keyp), sizeof(char))))
{
goto err;
}
memcpy(returnp, keyp, (keyp_end - keyp));
}
return(returnp);
err:
return(NULL);
}