Skip to content

Commit 0ae617d

Browse files
committed
fix content read bug
1 parent d33327a commit 0ae617d

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

proxylab-handout/dstring.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,30 @@ inline static size_t ceil_divide(size_t a, size_t b)
3636
void string_append(string *pstr, const char *cstr)
3737
{
3838
size_t cstr_len = strlen(cstr);
39+
string_appendn(pstr, cstr, cstr_len);
40+
}
41+
42+
void string_appendn(string *pstr, const char *cstr, size_t cstr_len)
43+
{
3944
if (pstr->len + cstr_len + 1 <= pstr->size) {
40-
strcpy(&pstr->cstr[pstr->len], cstr);
45+
for (int i = 0; i < cstr_len; i++) {
46+
pstr->cstr[pstr->len+i] = cstr[i];
47+
}
4148
pstr->len += cstr_len;
4249
} else {
43-
size_t multiplier = ceil_divide((cstr_len + pstr->len + 1) , pstr->size);
44-
size_t new_size = multiplier * pstr->size;
50+
size_t multipler = ceil_divide((cstr_len + pstr->len + 1),
51+
pstr->size);
52+
size_t new_size = multipler * pstr->size;
4553
char *new_str = (char *)malloc(new_size*sizeof(char));
46-
strcpy(new_str, pstr->cstr);
47-
strcpy(&new_str[pstr->len], cstr);
48-
pstr->cstr = new_str;
54+
for (int i = 0; i < pstr->len; i++) {
55+
new_str[i] = pstr->cstr[i];
56+
}
57+
for (int i = 0; i < cstr_len; i++) {
58+
new_str[pstr->len+i] = cstr[i];
59+
}
4960
pstr->len += cstr_len;
5061
pstr->size = new_size;
62+
pstr->cstr = new_str;
5163
}
64+
pstr->cstr[pstr->len] ='\0';
5265
}

proxylab-handout/dstring.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ void string_malloc(string *pstr);
1919
void string_free(string *pstr);
2020

2121
void string_append(string *pstr, const char *cstr);
22+
void string_appendn(string *pstr, const char *cstr, size_t cstr_len);
2223

2324
#endif

proxylab-handout/proxy.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3+
#include <assert.h>
34
#include "csapp.h"
45
#include "dstring.h"
56
#include "util.h"
@@ -48,12 +49,48 @@ void forward_response(int infd, int outfd)
4849
struct string response;
4950
string_malloc(&response);
5051

51-
while (Rio_readlineb(&rio, buf, MAXLINE)) {
52-
// printf("%s\n", buf);
52+
if (!Rio_readlineb(&rio, buf, MAXLINE)) {
53+
//TODO: error report
54+
return;
55+
}
56+
57+
string_append(&response, buf);
58+
59+
Rio_readlineb(&rio, buf, MAXLINE);
60+
char header_name[MAXLINE], header_value[MAXLINE];
61+
int content_length = 0;
62+
while (strcmp(buf, "\r\n")) {
63+
parse_header(buf, header_name, header_value);
64+
if (strcasecmp(header_name, "Content-length") == 0) {
65+
content_length = atoi(header_value);
66+
}
5367
string_append(&response, buf);
68+
Rio_readlineb(&rio, buf, MAXLINE);
5469
}
70+
string_append(&response, buf);
71+
72+
if (content_length == 0) {
73+
// TODO: error handling
74+
return;
75+
} else {
76+
#ifdef DEBUG
77+
fprintf(stderr, "Content-length: %d\n", content_length);
78+
#endif
79+
// read content
80+
char *content_buf = (char *)malloc((content_length+1)*sizeof(char));
81+
Rio_readnb(&rio, content_buf, content_length);
82+
string_appendn(&response, content_buf, (size_t)content_length);
83+
free(content_buf);
84+
}
85+
if (Rio_readlineb(&rio, buf, MAXLINE) != 0) {
86+
// TODO: error handling
87+
return;
88+
}
89+
90+
5591
#ifdef DEBUG
56-
fprintf(stderr, "response:\n%s", string_cstr(response));
92+
fprintf(stderr, "response length: %zu\n", string_length(response));
93+
// fprintf(stderr, "response:\n%s", string_cstr(response));
5794
#endif
5895

5996
Rio_writen(outfd,

0 commit comments

Comments
 (0)