Added function to stringify headers from linked list.

This commit is contained in:
ajuaristi 2014-05-22 09:51:35 +02:00
commit c466657496

View file

@ -106,6 +106,7 @@ int add_header(char *header, char *value){
// head is NULL, so the list is empty
ptr_head = new_ptr;
// TODO Remove the debug messages
hydra_report_debug(stdout, "[add_header] Added header %s: %s", header, value);
hydra_report_debug(stdout, "[add_header] len(header) = %d", strlen(header));
hydra_report_debug(stdout, "[add_header] len(value) = %d", strlen(value));
@ -137,9 +138,48 @@ int add_or_replace_header(char *header, char *value){
if(cur_ptr == NULL)
return add_header(header, value);
// TODO Remove the debug messages
hydra_report_debug(stdout, "[add_or_replace_header] Added header %s: %s", header, value);
hydra_report_debug(stdout, "[add_or_replace_header] len(header) = %d", strlen(header));
hydra_report_debug(stdout, "[add_or_replace_header] len(value) = %d", strlen(value));
return 1;
}
/*
* Concat all the headers in the list in a single string, and clean the whole list.
*/
char * stringify_headers_and_clean(){
char * headers_str = NULL;
ptr_header_node cur_ptr = ptr_head, tmp_ptr = NULL;
int ttl_size = 0;
while(cur_ptr){
if(cur_ptr->header && cur_ptr->value){ // Check for NULLs
ttl_size += strlen(cur_ptr->header) + strlen(cur_ptr->value) + 1;
headers_str = (char *) realloc(headers_str, sizeof(char) * ttl_size);
if(headers_str){ // Check for errors
strcat(headers_str, cur_ptr->header);
strcat(headers_str, ": ");
strcat(headers_str, cur_ptr->value);
strcat(headers_str, "\r\n");
}else{
// Error: out of memory
hydra_report(stderr, "Out of memory for HTTP headers");
hydra_child_exit(1);
}
}
// Clean it up and get to the next header
tmp_ptr = cur_ptr;
cur_ptr = cur_ptr->next;
free(tmp_ptr->header);
free(tmp_ptr->value);
free(tmp_ptr);
}
return headers_str;
}
int strpos(char *str, char *target) {
char *res = strstr(str, target);
@ -660,7 +700,7 @@ void service_http_form(char *ip, int sp, unsigned char options, char *miscptr, F
*ptr2++ = 0;
/*
* At this point:
* - optional1 contains the header's name
* - (optional1 + 2) contains the header's name
* - ptr contains the header's value
*/
header = (char *) malloc(strlen(optional1 + 2));
@ -668,10 +708,7 @@ void service_http_form(char *ip, int sp, unsigned char options, char *miscptr, F
if(header && value){
strcpy(header, optional1 + 2);
strcpy(value, hydra_strrep(ptr, "\\:", ":"));
hydra_report_debug(stdout, "Adding header: %s: %s", header, value);
hydra_report_debug(stdout, "\tlen(header): %d", strlen(header));
hydra_report_debug(stdout, "\tlen(value): %d", strlen(value));
if(add_header(header, value)){
if(add_header(hydra_trim(header), hydra_trim(value))){
// Success: break the switch and go ahead
optional1 = ptr2;
break;
@ -683,14 +720,21 @@ void service_http_form(char *ip, int sp, unsigned char options, char *miscptr, F
break;
case 'H':
// add a new header, or replace an existing one's value
// if (sizeof(userheader) - strlen(userheader) > 4) {
// strncat(userheader, optional1 + 2, sizeof(userheader) - strlen(userheader) - 4);
// strcat(userheader, ":");
// strncat(userheader, hydra_strrep(ptr, "\\:", ":"), sizeof(userheader) - strlen(userheader) - 3);
// strcat(userheader, "\r\n");
// }
header = (char *) malloc(strlen(optional1 + 2));
value = (char *) malloc(strlen(ptr));
if(header && value){
strcpy(header, optional1 + 2);
strcpy(value, hydra_strrep(ptr, "\\:", ":"));
if(add_or_replace_header(hydra_trim(header), hydra_trim(value))){
// Success: break the switch and go ahead
optional1 = ptr2;
break;
}
}
// Error: abort execution
hydra_report(stderr, "[ERROR] Out of memory for HTTP headers.");
hydra_child_exit(1);
break;
// no default
}
}