summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nanohttp/nanohttp-client.c81
-rw-r--r--nanohttp/nanohttp-socket.c10
-rw-r--r--nanohttp/nanohttp-socket.h4
3 files changed, 48 insertions, 47 deletions
diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c
index 8f3f37b..e31a171 100644
--- a/nanohttp/nanohttp-client.c
+++ b/nanohttp/nanohttp-client.c
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-client.c,v 1.2 2003/12/16 13:16:11 snowdrop Exp $
+ * $Id: nanohttp-client.c,v 1.3 2003/12/16 14:12:57 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -382,7 +382,7 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
httpc_response_callback cb, void *userdata)
{
hurl_t *url;
- char buffer[255];
+ char buffer[4096];
int status;
char *response;
int rsize;
@@ -390,6 +390,7 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
char *rest;
int restsize;
httpc_cb_userdata_t cbdata;
+ int i;
/* content-length */
char *content_length_str;
@@ -458,13 +459,43 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
}
/* Receive Response incl. header */
+ rsize = restsize = 0;
+ response = rest = NULL;
- status = hsocket_recv_limit(conn->sock, &response,
- "\r\n\r\n", &rest, &rsize, &restsize);
- if (status != HSOCKET_OK) {
+ status = hsocket_read(conn->sock, buffer, HSOCKET_MAX_BUFSIZE*1, 0);
+ if (status <= 0) {
log_error2("Can not receive response (status:%d)", status);
return 6;
}
+
+ for (i=0;i<status-3;i++) {
+
+ if (buffer[i] == '\n') {
+ if (buffer[i+1] == '\n') {
+ rsize = i;
+ response = buffer;
+ response[rsize] = '\0';
+
+ restsize = status - rsize - 2;
+ rest = &buffer[i+2];
+ rest[restsize] = '\0';
+ } else if (buffer[i+1] == '\r' && buffer[i+2] == '\n') {
+
+ rsize = i;
+ response = buffer;
+ response[rsize] = '\0';
+
+ restsize = status - rsize - 3;
+ rest = &buffer[i+3];
+ rest[restsize] = '\0'; /* REST WILL BE FREED IN BUFFEREDSOCKET !!!! */
+ }
+ }
+ }
+
+ if (response == NULL) {
+ log_error1("Header too long!");
+ return 13;
+ }
res = hresponse_new(response);
if (res == NULL) {
@@ -504,7 +535,7 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
recvSize = remain_length;
}
- if (hsocket_read(conn->sock, readBuf, recvSize)) {
+ if (hsocket_read(conn->sock, readBuf, recvSize,1)) {
log_error1("Can not read from socket!");
return 9;
} else {
@@ -516,8 +547,6 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
} /* while */
/* rest and response are no longer required */
- free(rest);
- free(response);
return 0;
@@ -536,38 +565,6 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
log_debug1("Server communicates with chunked encoding !");
- /* read chunk size */
- /*
- strncpy(chunk_size_str, rest, 15);
- chunk_size_cur = 0;
- cs = 0;
- while (1) {
-
- chunk_size_str[chunk_size_cur] = rest[cs++];
-
- if (chunk_size_str[chunk_size_cur] == '\n') {
- chunk_size_str[chunk_size_cur] = '\0';
- break;
- }
-
- if (chunk_size_str[chunk_size_cur] != '\r'
- && chunk_size_str[chunk_size_cur] != ';') {
- chunk_size_cur++;
- }
-
- if (chunk_size_cur > 15) {
- log_error1("Can not parse chunk size!");
- return 10;
- }
-
- }
-
- chunk_size = strtol(chunk_size_str,(char**)NULL, 16);
-
- log_debug3("chunk_size: '%s' as dec: '%d'",
- chunk_size_str, chunk_size);
- */
-
/* initialize buffered socket */
bufsock.sock = conn->sock;
bufsock.cur = 0;
@@ -612,11 +609,9 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr,
cb(counter++, conn, userdata, chunk_size, chunk_buffer);
free(chunk_buffer);
- } /* while (chunk_size > 0)
+ } /* while (chunk_size > 0) */
/* rest and response are no longer required */
- free(rest);
- free(response);
return 0;
diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c
index d465b8e..9974e76 100644
--- a/nanohttp/nanohttp-socket.c
+++ b/nanohttp/nanohttp-socket.c
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-socket.c,v 1.2 2003/12/16 13:16:14 snowdrop Exp $
+ * $Id: nanohttp-socket.c,v 1.3 2003/12/16 14:12:58 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -172,6 +172,7 @@ int hsocket_recv_limit(hsocket_t sock, char** buffer,
do {
+ log_debug1("recv()");
size = recv(sock, tmp, HSOCKET_MAX_BUFSIZE, 0);
if (size == -1) {
@@ -183,10 +184,12 @@ int hsocket_recv_limit(hsocket_t sock, char** buffer,
break;
}
+ puts(tmp);
/* section 1: find delimiter if exist */
for (i=0;i<size-delimlen;i++) {
if (!strncmp(&tmp[i],delim,delimlen)) {
+ log_debug1("Found delimiter");
/* ** split to buffer and rest ** */
/* fill buffer until i */
@@ -234,7 +237,7 @@ int hsocket_recv_limit(hsocket_t sock, char** buffer,
return HSOCKET_OK;
}
-int hsocket_read(hsocket_t sock, char* buffer, int total)
+int hsocket_read(hsocket_t sock, char* buffer, int total, int force)
{
int status;
int totalRead;
@@ -243,6 +246,7 @@ int hsocket_read(hsocket_t sock, char* buffer, int total)
do {
status = recv(sock, &buffer[totalRead], total - totalRead, 0);
+ if (!force) return status;
if (status > 0) {
totalRead += status;
} else {
@@ -381,6 +385,8 @@ int hbufsocket_read(hbufsocket_t *bufsock, char *buffer, int size)
bufsock->bufsize = size;
bufsock->cur = size;
strncpy(&buffer[tmpsize], bufsock->buffer, size);
+ } else {
+ return status;
}
return HSOCKET_OK;
diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h
index 901876f..f5f2d18 100644
--- a/nanohttp/nanohttp-socket.h
+++ b/nanohttp/nanohttp-socket.h
@@ -1,5 +1,5 @@
/******************************************************************
- * $Id: nanohttp-socket.h,v 1.2 2003/12/16 13:16:14 snowdrop Exp $
+ * $Id: nanohttp-socket.h,v 1.3 2003/12/16 14:12:58 snowdrop Exp $
*
* CSOAP Project: A http client/server library in C
* Copyright (C) 2003 Ferhat Ayaz
@@ -124,7 +124,7 @@ int hsocket_recv_limit(hsocket_t sock, char** buffer,
int hsocket_recv_cb(hsocket_t sock,
hsocket_recv_callback cb, void *userdata);
-int hsocket_read(hsocket_t sock, char* buffer, int total);
+int hsocket_read(hsocket_t sock, char* buffer, int total, int force);
/* ======================================== */