diff --git a/c/httphead/httphead.c b/c/httphead/httphead.c index 1e3c1d5..d3228e4 100644 --- a/c/httphead/httphead.c +++ b/c/httphead/httphead.c @@ -101,6 +101,10 @@ char* getpath ( const char* url ); char* getstatuscode ( const char* response ); char* b64 ( const char* str ); +void sendrequest(int stream, const char* path, const char* host, + const char* port, const char* authstring, const char* useragent, + const char* accept, const char* acceptencoding, + const char* acceptcharset, const char* acceptlanguage); int main ( int argc, char* argv[] ) { @@ -221,6 +225,10 @@ authstring = b64(authstring); } + if ( nouseragent ) { + useragent = NULL; + } + remote_hostent = gethostbyname(host); if ( remote_hostent == NULL ) { @@ -251,108 +259,12 @@ /* main */ if ( showrequest ) { - write(1, "GET ", 4); - write(1, path, strlen(path)); - write(1, " HTTP/1.0", 9); - crlf(1); - write(1, "Host: ", 6); - write(1, host, strlen(host)); - if ( port != NULL ) { - write(1, ":", 1); - write(1, port, strlen(port)); - } - crlf(1); - - if ((username != NULL) || (password != NULL)) { - write(1, "Authorization: Basic ", 21); - write(1, authstring, strlen(authstring)); - crlf(1); - } - - if ( !nouseragent ) { - write(1, "User-Agent: ", 12); - write(1, useragent, strlen(useragent)); - crlf(1); - } - - if ( accept != NULL ) { - write(1, "Accept: ", 8); - write(1, accept, strlen(accept)); - crlf(1); - } - - if ( acceptencoding != NULL ) { - write(1, "Accept-Encoding: ", 17); - write(1, acceptencoding, strlen(acceptencoding)); - crlf(1); - } - - if ( acceptcharset != NULL ) { - write(1, "Accept-Charset: ", 16); - write(1, acceptcharset, strlen(acceptcharset)); - crlf(1); - } - - if ( acceptlanguage != NULL ) { - write(1, "Accept-Language: ", 17); - write(1, acceptlanguage, strlen(acceptlanguage)); - crlf(1); - } - - crlf(1); - + sendrequest(1, path, host, port, authstring, useragent, accept, acceptencoding, acceptcharset, acceptlanguage); write(1, "Response:\n\n", 12); - } - - write(sock, "GET ", 4); - write(sock, path, strlen(path)); - write(sock, " HTTP/1.0", 9); - crlf(sock); - write(sock, "Host: ", 6); - write(sock, host, strlen(host)); - if ( port != NULL ) { - write(sock, ":", 1); - write(sock, port, strlen(port)); - } - crlf(sock); - - if ((username != NULL) || (password != NULL)) { - write(sock, "Authorization: Basic ", 21); - write(sock, authstring, strlen(authstring)); - crlf(sock); } - if ( !nouseragent ) { - write(sock, "User-Agent: ", 12); - write(sock, useragent, strlen(useragent)); - crlf(sock); - } + sendrequest(sock, path, host, port, authstring, useragent, accept, acceptencoding, acceptcharset, acceptlanguage); - if ( accept != NULL ) { - write(sock, "Accept: ", 8); - write(sock, accept, strlen(accept)); - crlf(sock); - } - - if ( acceptencoding != NULL ) { - write(sock, "Accept-Encoding: ", 17); - write(sock, acceptencoding, strlen(acceptencoding)); - crlf(sock); - } - - if ( acceptcharset != NULL ) { - write(sock, "Accept-Charset: ", 16); - write(sock, acceptcharset, strlen(acceptcharset)); - crlf(sock); - } - - if ( acceptlanguage != NULL ) { - write(sock, "Accept-Language: ", 17); - write(sock, acceptlanguage, strlen(acceptlanguage)); - crlf(sock); - } - - crlf(sock); while ( (tmp = recv(sock, buff, HH_BUFFSIZE, 0)) > 0 ) { buff[tmp] = '\0'; @@ -391,7 +303,8 @@ exit(0); } -char* _gethoststart ( const char* url ) { +char* _gethoststart ( const char* url ) +{ char* s = NULL; s = strstr(url, "http://"); @@ -413,7 +326,8 @@ return s; } -int _gethostlen ( const char* s ) { +int _gethostlen ( const char* s ) +{ int e = 0; while ( *(s+e) != '/' && *(s+e) != '?' && *(s+e) != '\0' ) { @@ -423,7 +337,8 @@ return e; } -char* getstatuscode ( const char* s ) { +char* getstatuscode ( const char* s ) +{ char* htp = NULL; if ( (htp = strstr(s, "HTTP/")) != NULL ) { @@ -443,7 +358,8 @@ return htp; } -char* getpath ( const char* url ) { +char* getpath ( const char* url ) +{ char* s = NULL; int e; @@ -462,7 +378,8 @@ return s; } -char* getpassword ( const char* url ) { +char* getpassword ( const char* url ) +{ char* s = NULL; char* r = NULL; int e = 0; @@ -494,7 +411,8 @@ return r; } -char* getuser ( const char* url ) { +char* getuser ( const char* url ) +{ char* s = NULL; char* r = NULL; int e = 0; @@ -530,7 +448,8 @@ return r; } -char* gethost ( const char* url ) { +char* gethost ( const char* url ) +{ char* s = NULL; char* r = NULL; int e = 0; @@ -567,7 +486,8 @@ return r; } -char* getport ( const char* url ) { +char* getport ( const char* url ) +{ char* s = NULL; char* r = NULL; int e = 0; @@ -608,14 +528,16 @@ } -void _b64_43 ( char* dst, const char* src ) { +void _b64_43 ( char* dst, const char* src ) +{ dst[0] = b64chars[src[0] >> 2]; dst[1] = b64chars[(((src[0] & 0x03) << 4) & 0xF0) | (src[1] >> 4)]; dst[2] = b64chars[((src[1] << 2) & 0x3C) | ((src[2] >> 6) & 0x03)]; dst[3] = b64chars[src[2] & 0x3F]; } -char* b64 ( const char* str ) { +char* b64 ( const char* str ) +{ int l = strlen(str); char* out = NULL; int pos = 0; @@ -657,27 +579,89 @@ return out; } -void crlf ( int s ) { +void crlf ( int s ) +{ write(s, "\x0D\x0A", 2); } -void usage ( ) { +void usage ( ) +{ write(1, usagemsg, strlen(usagemsg)); } -void license ( ) { +void license ( ) +{ write(1, licensemsg, strlen(licensemsg)); } -void version ( ) { +void version ( ) +{ write(1, versionmsg, strlen(versionmsg)); write(1, "\n", 1); } -void showerror ( const char* type ) { +void showerror ( const char* type ) +{ char* errstr = strerror(errno); write(2, type, strlen(type)); write(2, ": ", 2); write(2, errstr, strlen(errstr)); write(2, "\n", 1); } + +void sendrequest ( int stream, const char* path, const char* host, + const char* port, const char* authstring, const char* useragent, + const char* accept, const char* acceptencoding, + const char* acceptcharset, const char* acceptlanguage ) +{ + + write(stream, "GET ", 4); + write(stream, path, strlen(path)); + write(stream, " HTTP/1.0", 9); + crlf(stream); + write(stream, "Host: ", 6); + write(stream, host, strlen(host)); + if ( port != NULL ) { + write(stream, ":", 1); + write(stream, port, strlen(port)); + } + crlf(stream); + + if ( authstring != NULL ) { + write(stream, "Authorization: Basic ", 21); + write(stream, authstring, strlen(authstring)); + crlf(stream); + } + + if ( useragent != NULL ) { + write(stream, "User-Agent: ", 12); + write(stream, useragent, strlen(useragent)); + crlf(stream); + } + + if ( accept != NULL ) { + write(stream, "Accept: ", 8); + write(stream, accept, strlen(accept)); + crlf(stream); + } + + if ( acceptencoding != NULL ) { + write(stream, "Accept-Encoding: ", 17); + write(stream, acceptencoding, strlen(acceptencoding)); + crlf(stream); + } + + if ( acceptcharset != NULL ) { + write(stream, "Accept-Charset: ", 16); + write(stream, acceptcharset, strlen(acceptcharset)); + crlf(stream); + } + + if ( acceptlanguage != NULL ) { + write(stream, "Accept-Language: ", 17); + write(stream, acceptlanguage, strlen(acceptlanguage)); + crlf(stream); + } + + crlf(stream); +}