diff --git a/c/httphead/httphead.c b/c/httphead/httphead.c index 349d617..1e3c1d5 100644 --- a/c/httphead/httphead.c +++ b/c/httphead/httphead.c @@ -37,18 +37,20 @@ #include const char usagemsg[] = - "usage: httphead [-r] [-q] [-u username:password] [-p port] [-s host] [-d path] [-s host] URL\n" + "usage: httphead [-r] [-q] [-n] [-u ua] [-a as] [-e ae] [-c ac] [-l al] URL\n" "\n" "options:\n" - " -p port use this port instead of 80\n" - " -s host use this host instead of the one specified in the URL\n" - " -d path use this path instead of the one specified in the URL\n" - " -u user:passwd authenticate using a username and a password\n" " -r show sent request\n" " -q show only the recieved status code\n" + " -n don't send User-Agent\n" + " -u useragent send User-Agent: useragent\n" + " -a acceptstr send Accept: acceptstr\n" + " -e acceptenc send Accept-Encoding: acceptenc\n" + " -c acceptchs send Accept-Charset: acceptchs\n" + " -l acceptlng send Accept-Language: acceptlng\n" " also: -v show version\n" " -h display this help\n" - " -l display (BSD) license\n" + " -b display (BSD) license\n" ; const char licensemsg[] = @@ -78,7 +80,7 @@ "THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" ; -const char versionmsg[] = "httphead 0.3\n"; +const char versionmsg[] = "httphead 0.6"; const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -109,6 +111,12 @@ char* url = NULL; + char* useragent = (char*)versionmsg; + char* accept = NULL; + char* acceptencoding = NULL; + char* acceptcharset = NULL; + char* acceptlanguage = NULL; + char* host = NULL; char* path = NULL; char* username = NULL; @@ -116,6 +124,7 @@ char* port = NULL; int showrequest = 0; + int nouseragent = 0; int statuscodeonly = 0; char* statuscode = NULL; @@ -125,26 +134,17 @@ int sock; - while ( (tmp = getopt(argc, argv, "u:qrd:s:p:hlv")) != -1 ) { + while ( (tmp = getopt(argc, argv, "l:c:e:a:nu:qrhbv")) != -1 ) { switch ( tmp ) { case 'h': usage(); exit(0); - case 'l': + case 'b': license(); exit(0); case 'v': version(); exit(0); - case 'p': - port = optarg; - break; - case 's': - host = optarg; - break; - case 'd': - path = optarg; - break; case 'q': statuscodeonly = 1; break; @@ -152,21 +152,22 @@ showrequest = 1; break; case 'u': - username = optarg; - - if ((password = strchr(username, ':')) == username) { - username = NULL; - } - - if (password != NULL) { - *password = '\0'; - - if (*(password+1) != '\0') { - password++; - } else { - password = NULL; - } - } + useragent = optarg; + break; + case 'n': + nouseragent = 1; + break; + case 'a': + accept = optarg; + break; + case 'e': + acceptencoding = optarg; + break; + case 'c': + acceptcharset = optarg; + break; + case 'l': + acceptlanguage = optarg; break; } } @@ -178,32 +179,25 @@ url = argv[optind]; - if ( path == NULL ) { - path = getpath(url); - } + path = getpath(url); - if ( host == NULL ) { - host = gethost(url); - } + host = gethost(url); - if ( port == NULL ) { - port = getport(url); - } + port = getport(url); - if ( username == NULL ) { - username = getuser(url); - } + username = getuser(url); - if ( password == NULL ) { - password = getpassword(url); - } + password = getpassword(url); if ((username != NULL) || (password != NULL)) { - tmp = 0; if ( username != NULL ) { l = strlen(username); - tmp += l; + tmp = l; + } else { + tmp = 0; + l = 0; } + tmp++; if ( password != NULL ) { @@ -212,9 +206,15 @@ authstring = (char*)alloca(sizeof(char)*(tmp+1)); - memcpy(authstring, username, l); + if ( username != NULL ) { + memcpy(authstring, username, l); + } + authstring[l] = ':'; - memcpy(authstring+l+1, password, strlen(password)); + + if ( password != NULL ) { + memcpy(authstring+l+1, password, strlen(password)); + } authstring[tmp] = '\0'; @@ -269,6 +269,36 @@ 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); write(1, "Response:\n\n", 12); @@ -292,6 +322,36 @@ crlf(sock); } + if ( !nouseragent ) { + write(sock, "User-Agent: ", 12); + write(sock, useragent, strlen(useragent)); + crlf(sock); + } + + 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 ) { @@ -611,6 +671,7 @@ void version ( ) { write(1, versionmsg, strlen(versionmsg)); + write(1, "\n", 1); } void showerror ( const char* type ) {