#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); } /* 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) { char *buf; /* data indices */ char *d_ind, *d_ind2; buf = NULL; d_ind = d_ind2 = data; /* 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) { free(buf); buf = NULL; for(; '\n' != *(data - 1); data++); d_ind = d_ind2 = data; } for(; *d_ind != ':'; d_ind++); buf = substr_extract(data, 0, (d_ind - data)); buf = buftolower(buf); 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); } } return(NULL); }