Skip to content

Commit

Permalink
bugfix: I forgot to change the content-length when I did a replacemen…
Browse files Browse the repository at this point in the history
…t, leading to timeouts
  • Loading branch information
reinhrst committed Dec 31, 2013
1 parent e077056 commit e4145ad
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
18 changes: 17 additions & 1 deletion notify-forwarder.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,23 @@ int main(int argc, char** argv) {
printf("strange, no request.... %s\n", strerror(errno));
continue;
}
char* send_buffer = str_replace(request, sonos_box_ip_address, outside_nat_ip_address);
char* send_buffer = str_replace(request, sonos_box_ip_address, outside_nat_ip_address, STR_REPLACE_REPLACE_ALL);
long lengthdifference = strlen(send_buffer) - strlen(request);
if (lengthdifference) {
//update the content-length header
int oldlength = get_content_length(send_buffer);
char* cl_start = strstr(send_buffer, CONTENT_LENGTH_LINE);
char* cl_end = strstr(cl_start + strlen(CONTENT_LENGTH_LINE), "\r\n");
char* replace_from = calloc(cl_end - cl_start + 1, sizeof(char));
strncpy(replace_from, cl_start, cl_end-cl_start);
char* replace_with;
asprintf(&replace_with, "%s %ld", CONTENT_LENGTH_LINE, oldlength + lengthdifference);
char* new_send_buffer = str_replace(send_buffer, replace_from, replace_with, 1);
free(replace_from);
free(replace_with);
free(send_buffer);
send_buffer = new_send_buffer;
}
char* host = get_host(send_buffer);
unsigned short port = get_port(send_buffer);
fill_socketaddr_in(&forward_destination_address, port, host);
Expand Down
2 changes: 1 addition & 1 deletion sonos-forwarder.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int main(int argc, char** argv) {
printf("no reply\n");
} else {
int send_buffer_size = nr_bytes_received - strlen(to_replace) + strlen(replace_with) + 1;
char* send_buffer = str_replace(receive_buffer, to_replace, replace_with);
char* send_buffer = str_replace(receive_buffer, to_replace, replace_with, STR_REPLACE_REPLACE_ALL);
sendto(forward_reply_socket, send_buffer, send_buffer_size, 0, (struct sockaddr*) &remote_address, remote_address_length);
free(send_buffer);
printf("Message %s <-- internal\n", inet_ntoa(remote_address.sin_addr));
Expand Down
9 changes: 8 additions & 1 deletion str_replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdlib.h>

// You must free the result if result is non-NULL.
char *str_replace(const char *orig, const char *rep, const char *with) {
char *str_replace(const char *orig, const char *rep, const char *with, unsigned int max_replacements) {
char *result; // the return string
const char *ins; // the next insert point
char *tmp; // varies
Expand All @@ -17,6 +17,9 @@ char *str_replace(const char *orig, const char *rep, const char *with) {
if (!rep)
rep = "";
len_rep = strlen(rep);
if (!len_rep) {
return NULL; //undefined, how can we replace nothing with something
}
if (!with)
with = "";
len_with = strlen(with);
Expand All @@ -26,6 +29,10 @@ char *str_replace(const char *orig, const char *rep, const char *with) {
ins = tmp + len_rep;
}

if (max_replacements != 0 && count > max_replacements) {
count = max_replacements;
}

// first time through the loop, all the variable are set correctly
// from here on,
// tmp points to the end of the result string
Expand Down
7 changes: 6 additions & 1 deletion str_replace.h
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
char *str_replace(const char *orig, const char *rep, const char *with);
/**
* Replaces occurences of one string with the next. Returns a new char* which needs to be free'd later
* if max_replacements == STR_REPLACE_REPLACE_ALL, then all occurences are replaced
**/
#define STR_REPLACE_REPLACE_ALL 0
char *str_replace(const char *orig, const char *rep, const char *with, unsigned int max_replacements);

0 comments on commit e4145ad

Please sign in to comment.