diff options
author | LAN-TW <lantw44@gmail.com> | 2013-10-13 03:29:33 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-10-13 03:29:33 +0800 |
commit | 764ac76c7339b6f9ebc3d13ad3de9e8bd9ef750a (patch) | |
tree | 1d239b4297ff4bf8817446377e5b2a317c2720b5 | |
parent | a7977c287bac2d19db212158df05bc47c96110ad (diff) | |
download | sp2013-764ac76c7339b6f9ebc3d13ad3de9e8bd9ef750a.tar.gz sp2013-764ac76c7339b6f9ebc3d13ad3de9e8bd9ef750a.tar.zst sp2013-764ac76c7339b6f9ebc3d13ad3de9e8bd9ef750a.zip |
HW1: 避免因 client 未輸入換行而導致程式結束
-rw-r--r-- | hw1/common.c | 10 | ||||
-rw-r--r-- | hw1/common.h | 1 | ||||
-rw-r--r-- | hw1/server.c | 27 |
3 files changed, 34 insertions, 4 deletions
diff --git a/hw1/common.c b/hw1/common.c index ef48ef1..dcfed75 100644 --- a/hw1/common.c +++ b/hw1/common.c @@ -21,6 +21,16 @@ void* e_malloc (size_t size) { return ptr; } +char* e_strcat (const char* s1, const char* s2) { + int s1len = strlen (s1); + int s2len = strlen (s2); + char* newstr = e_malloc (sizeof (char) * (s1len + s2len + 1)); + strcpy (newstr, s1); + strcpy (newstr + s1len, s2); + newstr[s1len + s2len] = '\0'; + return newstr; +} + void e_err_exit (const char* a) { perror (a); exit (1); diff --git a/hw1/common.h b/hw1/common.h index b580098..d219121 100644 --- a/hw1/common.h +++ b/hw1/common.h @@ -9,6 +9,7 @@ #include <unistd.h> void* e_malloc (size_t size); +char* e_strcat (const char* s1, const char* s2); void e_err_exit (const char* a); typedef struct { diff --git a/hw1/server.c b/hw1/server.c index 2478eb3..91646b7 100644 --- a/hw1/server.c +++ b/hw1/server.c @@ -119,12 +119,31 @@ int request_read(request* reqP) { if (p1 == NULL) { p1 = strchr(buf, '\n'); newline_len = 1; - assert (p1 != NULL); + if (p1 == NULL) { + char* pfn = e_malloc (sizeof (char) * (r + 1)); + memmove (pfn, buf, r); + pfn[r] = '\0'; + if (reqP->filename == NULL) { + reqP->filename = pfn; + } else { + char* newpfn = e_strcat (reqP->filename, pfn); + free (reqP->filename); + reqP->filename = newpfn; + } + return 1; + } } size_t len = p1 - buf + 1; - reqP->filename = (char*)e_malloc(len); - memmove(reqP->filename, buf, len - 1); - reqP->filename[len - 1] = '\0'; + char* fn = (char*)e_malloc(len); + memmove(fn, buf, len - 1); + fn[len - 1] = '\0'; + if (reqP->filename == NULL) { + reqP->filename = fn; + } else { + char* newfn = e_strcat (reqP->filename, fn); + free (reqP->filename); + reqP->filename = newfn; + } p1 += newline_len; reqP->buf_len = r - (p1 - buf); memmove(reqP->buf, p1, reqP->buf_len); |