From 49f500d8b52edea7a9d4ed81c16e83674e04e120 Mon Sep 17 00:00:00 2001 From: snowdrop Date: Mon, 5 Jan 2004 10:42:14 +0000 Subject: startet post function --- config.h.in | 6 --- examples/nanohttp/Makefile.am | 15 +++--- nanohttp/nanohttp-client.c | 113 +++++++++++++++++++++++++++++++++++++----- nanohttp/nanohttp-client.h | 11 ++-- nanohttp/nanohttp-socket.c | 16 +++++- nanohttp/nanohttp-socket.h | 10 +++- 6 files changed, 141 insertions(+), 30 deletions(-) diff --git a/config.h.in b/config.h.in index 2e100bd..0356f7c 100644 --- a/config.h.in +++ b/config.h.in @@ -51,9 +51,6 @@ /* Define to 1 if you have the `vprintf' function. */ #undef HAVE_VPRINTF -/* Name of package */ -#undef PACKAGE - /* Define to the address where bug reports for this package should be sent. */ #undef PACKAGE_BUGREPORT @@ -78,9 +75,6 @@ /* Define to 1 if your declares `struct tm'. */ #undef TM_IN_SYS_TIME -/* Version number of package */ -#undef VERSION - /* Define to empty if `const' does not conform to ANSI C. */ #undef const diff --git a/examples/nanohttp/Makefile.am b/examples/nanohttp/Makefile.am index a5db7b3..221f469 100644 --- a/examples/nanohttp/Makefile.am +++ b/examples/nanohttp/Makefile.am @@ -1,13 +1,16 @@ -bin_PROGRAMS=httpget httpgetcb +bin_PROGRAMS=httpget httpgetcb httppost + +INCLUDES=-I$(top_srcdir)/ httpget_SOURCES=httpget.c -INCLUDES=-I$(top_srcdir)/ $(LIBXML_CFLAGS) -httpget_LDFLAGS=-L$(top_builddir)/nanohttp -lnanohttp-$(GENERIC_API_VERSION) \ -$(LIBXML_LIBS) +httpget_LDFLAGS=-L$(top_builddir)/nanohttp -lnanohttp-$(GENERIC_API_VERSION) httpgetcb_SOURCES=httpgetcb.c -httpgetcb_LDFLAGS=-L$(top_builddir)/nanohttp -lnanohttp-$(GENERIC_API_VERSION) \ -$(LIBXML_LIBS) +httpgetcb_LDFLAGS=-L$(top_builddir)/nanohttp -lnanohttp-$(GENERIC_API_VERSION) + +httppost_SOURCES=httppost.c +httppost_LDFLAGS=-L$(top_builddir)/nanohttp -lnanohttp-$(GENERIC_API_VERSION) + diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index f7d17a6..07c1d2d 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.c,v 1.7 2003/12/18 15:32:09 snowdrop Exp $ + * $Id: nanohttp-client.c,v 1.8 2004/01/05 10:42:15 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -541,10 +541,8 @@ int httpc_receive_response(httpc_conn_t *conn, >0 otherwise. ----------------------------------------------------*/ static -int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, const char *urlstr, - httpc_response_start_callback start_cb, - httpc_response_callback cb, int content_size, - char* content, void *userdata) +int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, + const char *urlstr) { hurl_t *url; char buffer[4096]; @@ -610,7 +608,6 @@ int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, const char *url return 5; } - status = httpc_receive_response(conn, start_cb, cb, userdata); return status; } @@ -630,8 +627,12 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr, { int status; - status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr, - start_cb, cb, 0, NULL, userdata); + status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr); + + if (status != HSOCKET_OK) + return status; + + status = httpc_receive_response(conn, start_cb, cb, userdata); return status; } @@ -644,6 +645,8 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr, See the documentation of httpc_talk_to_server() for more information. + + TODO: Add post content rutine ----------------------------------------------------*/ int httpc_post_cb(httpc_conn_t *conn, const char *urlstr, httpc_response_start_callback start_cb, @@ -651,11 +654,21 @@ int httpc_post_cb(httpc_conn_t *conn, const char *urlstr, char *content, void *userdata) { int status; + char buffer[255]; - status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, urlstr, - start_cb, cb, content_size, content, - userdata); - return status; + sprintf(buffer, "%d", content_size); + httpc_set_header(conn, HEADER_CONTENT_LENGTH, buffer); + + status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, urlstr); + if (status != HSOCKET_OK) + return status; + + status = hsocket_nsend(conn->sock, content, content_size); + if (status != HSOCKET_OK) + return status; + + status = httpc_receive_response(conn, start_cb, cb, userdata); + return status; } @@ -760,13 +773,89 @@ hresponse_t *httpc_post(httpc_conn_t *conn, const char *url, +/* + POST Module + */ +/*-------------------------------------------------- + FUNCTION: httpc_post_open +----------------------------------------------------*/ +int httpc_post_open(httpc_conn_t *conn, const char *urlstr) +{ + int status; + + httpc_set_header(conn, HEADER_TRANSFER_ENCODING, "chunked"); + status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, urlstr); + return status; +} +/*-------------------------------------------------- + FUNCTION: httpc_post_send +----------------------------------------------------*/ +int httpc_post_send(httpc_conn_t *conn, + const char* buffer, + int bufsize) +{ + char hexsize[100]; /* chunk size in hex */ + int status; + + sprintf(hexsize, "%x\r\n", bufsize); + + status = hsocket_send(conn->sock, hexsize); + if (status != HSOCKET_OK) + return status; + + status = hsocket_nsend(conn->sock, buffer, bufsize); + if (status != HSOCKET_OK) + return status; + status = hsocket_send(conn->sock, "\r\n"); + + return status; +} + + +/*-------------------------------------------------- + FUNCTION: httpc_post_finish +----------------------------------------------------*/ +hresponse_t *httpc_post_finish(httpc_conn_t *conn) +{ + int status; + hresponse_t *res; + + res = hresponse_new(); + status = httpc_post_finish_cb(conn, httpc_custom_start_callback, + httpc_custom_res_callback, res); + + if (status != 0) { + hresponse_free(res); + return NULL; + } + + return res; +} + + +/*-------------------------------------------------- + FUNCTION: httpc_post_finish_cb +----------------------------------------------------*/ +int httpc_post_finish_cb(httpc_conn_t *conn, + httpc_response_start_callback start_cb, + httpc_response_callback cb, + void *userdata) +{ + int status; + + status = hsocket_send(conn->sock, "0\r\n"); + if (status != HSOCKET_OK) return status; + + status = httpc_receive_response(conn, start_cb, cb, userdata); + return status; +} diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index ed249f8..19ae110 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.h,v 1.3 2003/12/18 15:32:09 snowdrop Exp $ + * $Id: nanohttp-client.h,v 1.4 2004/01/05 10:42:15 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -72,12 +72,15 @@ int httpc_post_cb(httpc_conn_t *conn, const char *url, -int httpc_post_open(httpc_conn_t *conn); -int httpc_post_send(httpc_conn_t *conn, const char* buffer); +/* + Chunked POST Module + */ +int httpc_post_open(httpc_conn_t *conn, const char *url); +int httpc_post_send(httpc_conn_t *conn, const char* buffer, int bufsize); hresponse_t *httpc_post_finish(httpc_conn_t *conn); int httpc_post_finish_cb(httpc_conn_t *conn, httpc_response_start_callback start_cb, - httpc_response_callback cb); + httpc_response_callback cb, void *userdata); #endif diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c index 9aaca65..18edcb9 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.c,v 1.4 2003/12/17 12:55:02 snowdrop Exp $ + * $Id: nanohttp-socket.c,v 1.5 2004/01/05 10:42:15 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -130,6 +130,20 @@ void hsocket_close(hsocket_t sock) } +/*-------------------------------------------------- + FUNCTION: hsocket_send +----------------------------------------------------*/ +int hsocket_nsend(hsocket_t sock, const char* buffer, int n) +{ + int size; + + size = send((int)sock, buffer, n, 0); + if (size == -1) + return HSOCKET_CAN_NOT_SEND; + + return HSOCKET_OK; +} + /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index f5f2d18..d73cf50 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.3 2003/12/16 14:12:58 snowdrop Exp $ + * $Id: nanohttp-socket.h,v 1.4 2004/01/05 10:42:15 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -71,6 +71,14 @@ int hsocket_open(hsocket_t *sock, const char* host, int port); void hsocket_close(hsocket_t sock); +/* + hsocket_nsend + sends n bytes of data + Returns 0 if success + >0 if fail + */ +int hsocket_nsend(hsocket_t sock, const char* buffer, int n); + /* hsocket_send sends strlen(buffer) bytes of data -- cgit v1.1-32-gdbae