diff --git a/Makefile b/Makefile index b3e1e57..1cc7548 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,8 @@ all: gcc -DGHTTP_MAJOR_VERSION=1 -DGHTTP_MINOR_VERSION=0 -DGHTTP_MICRO_VERSION=9 -DPACKAGE=\"libghttp\" -DVERSION=\"1.0.9\" -DSTDC_HEADERS=1 -I. -I. -g -O2 -c http_base64.c -o http_base64.o >/dev/null 2>&1 ar cru libghttp.a ghttp.o http_date.o http_hdrs.o http_req.o http_resp.o http_trans.o http_uri.o http_base64.o ranlib libghttp.a + cp libghttp.a ./lib/ + cp ghttp.h ghttp_constants.h ./lib/include/ clean: rm *.o *.a diff --git a/dynamic_library_test/Makefile b/dynamic_library_test/Makefile new file mode 100644 index 0000000..a90b7f9 --- /dev/null +++ b/dynamic_library_test/Makefile @@ -0,0 +1,8 @@ +all: + gcc -g -Wall -shared -fPIC -o librequestsend.so -I../lib/include/ -L../lib/ -lghttp http_request.c http_request.h + +with-debug: + gcc -g -Wall -DDEBUG_MSG -shared -fPIC -o librequestsend.so -I../lib/include/ -L../lib/ -lghttp http_request.c http_request.h + +clean: + rm *.so diff --git a/dynamic_library_test/Readme b/dynamic_library_test/Readme new file mode 100644 index 0000000..1f7657f --- /dev/null +++ b/dynamic_library_test/Readme @@ -0,0 +1,20 @@ +Range of mothod value: + +typedef enum ghttp_type_tag +{ + ghttp_type_get = 0, + ghttp_type_options, + ghttp_type_head, + ghttp_type_post, + ghttp_type_put, + ghttp_type_delete, + ghttp_type_trace, + ghttp_type_connect, + ghttp_type_propfind, + ghttp_type_proppatch, + ghttp_type_mkcol, + ghttp_type_copy, + ghttp_type_move, + ghttp_type_lock, + ghttp_type_unlock +} ghttp_type; diff --git a/dynamic_library_test/build_test_mothod.txt b/dynamic_library_test/build_test_mothod.txt new file mode 100644 index 0000000..0111f14 --- /dev/null +++ b/dynamic_library_test/build_test_mothod.txt @@ -0,0 +1 @@ +gcc -o test test.c http_request.h ./librequestsend.so ./libghttp.a diff --git a/dynamic_library_test/ghttp.h b/dynamic_library_test/ghttp.h new file mode 100755 index 0000000..08a68f6 --- /dev/null +++ b/dynamic_library_test/ghttp.h @@ -0,0 +1,263 @@ +/* + * ghttp.h -- A public interface to common http functions + * Created: Christopher Blizzard , 21-Aug-1998 + * + * Copyright (C) 1998 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GHTTP_H +#define GHTTP_H + +#include "ghttp_constants.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct _ghttp_request ghttp_request; + +typedef enum ghttp_type_tag +{ + ghttp_type_get = 0, + ghttp_type_options, + ghttp_type_head, + ghttp_type_post, + ghttp_type_put, + ghttp_type_delete, + ghttp_type_trace, + ghttp_type_connect, + ghttp_type_propfind, + ghttp_type_proppatch, + ghttp_type_mkcol, + ghttp_type_copy, + ghttp_type_move, + ghttp_type_lock, + ghttp_type_unlock +} ghttp_type; + +typedef enum ghttp_sync_mode_tag +{ + ghttp_sync = 0, + ghttp_async +} ghttp_sync_mode; + +typedef enum ghttp_status_tag +{ + ghttp_error = -1, + ghttp_not_done, + ghttp_done +} ghttp_status; + +typedef enum ghttp_proc_tag +{ + ghttp_proc_none = 0, + ghttp_proc_request, + ghttp_proc_response_hdrs, + ghttp_proc_response +} ghttp_proc; + +typedef struct ghttp_current_status_tag +{ + ghttp_proc proc; /* what's it doing? */ + int bytes_read; /* how many bytes have been read? */ + int bytes_total; /* how many total */ +} ghttp_current_status; + +/* create a new request object */ +ghttp_request * +ghttp_request_new(void); + +/* delete a current request object */ +void +ghttp_request_destroy(ghttp_request *a_request); + +/* Validate a uri + * This will return -1 if a uri is invalid + */ +int +ghttp_uri_validate(char *a_uri); + +/* Set a uri in a request + * This will return -1 if the uri is invalid + */ + +int +ghttp_set_uri(ghttp_request *a_request, char *a_uri); + +/* Set a proxy for a request + * This will return -1 if the uri is invalid + */ + +int +ghttp_set_proxy(ghttp_request *a_request, char *a_uri); + +/* Set a request type + * This will return -1 if the request type is invalid or + * unsupported + */ + +int +ghttp_set_type(ghttp_request *a_request, ghttp_type a_type); + +/* Set the body. + * This will return -1 if the request type doesn't support it + */ + +int +ghttp_set_body(ghttp_request *a_request, char *a_body, int a_len); + +/* Set whether or not you want to use sync or async mode. + */ + +int +ghttp_set_sync(ghttp_request *a_request, + ghttp_sync_mode a_mode); + +/* Prepare a request. + * Call this before trying to process a request or if you change the + * uri. + */ + +int +ghttp_prepare(ghttp_request *a_request); + +/* Set the chunk size + * You might want to do this to optimize for different connection speeds. + */ + +void +ghttp_set_chunksize(ghttp_request *a_request, int a_size); + +/* Set a random request header + */ + +void +ghttp_set_header(ghttp_request *a_request, + const char *a_hdr, const char *a_val); + +/* Process a request + */ + +ghttp_status +ghttp_process(ghttp_request *a_request); + +/* Get the status of a request + */ + +ghttp_current_status +ghttp_get_status(ghttp_request *a_request); + +/* Flush the received data (so far) into the response body. This is + * useful for asynchronous requests with large responses: you can + * periodically flush the response buffer and parse the data that's + * arrived so far. + */ + +void +ghttp_flush_response_buffer(ghttp_request *a_request); + +/* Get the value of a random response header + */ + +const char * +ghttp_get_header(ghttp_request *a_request, + const char *a_hdr); + +/* Get the list of headers that were returned in the response. You + must free the returned string values. This function will return 0 + on success, -1 on some kind of error. */ +int +ghttp_get_header_names(ghttp_request *a_request, + char ***a_hdrs, int *a_num_hdrs); + +/* Abort a currently running request. */ +int +ghttp_close(ghttp_request *a_request); + +/* Clean a request + */ +void +ghttp_clean(ghttp_request *a_request); + +/* Get the socket associated with a particular connection + */ + +int +ghttp_get_socket(ghttp_request *a_request); + +/* get the return entity body + */ + +char * +ghttp_get_body(ghttp_request *a_request); + +/* get the returned length + */ + +int +ghttp_get_body_len(ghttp_request *a_request); + +/* Get an error message for a request that has failed. + */ + +const char * +ghttp_get_error(ghttp_request *a_request); + +/* Parse a date string that is one of the standard + * date formats + */ + +time_t +ghttp_parse_date(char *a_date); + +/* Return the status code. + */ + +int +ghttp_status_code(ghttp_request *a_request); + +/* Return the reason phrase. + */ + +const char * +ghttp_reason_phrase(ghttp_request *a_request); + +/* Set your username/password pair + */ + +int +ghttp_set_authinfo(ghttp_request *a_request, + const char *a_user, + const char *a_pass); + + + /* Set your username/password pair for proxy + */ + +int +ghttp_set_proxy_authinfo(ghttp_request *a_request, + const char *a_user, + const char *a_pass); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* GHTTP_H */ diff --git a/dynamic_library_test/ghttp_constants.h b/dynamic_library_test/ghttp_constants.h new file mode 100755 index 0000000..7e1d07a --- /dev/null +++ b/dynamic_library_test/ghttp_constants.h @@ -0,0 +1,105 @@ +/* + * ghttp_constants.h -- definitions for char constants that people + * might want to use + * Created: Christopher Blizzard + * + * Copyright (C) 1998 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GHTTP_CONSTANTS_H +#define GHTTP_CONSTANTS_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +extern const char http_hdr_Allow[]; +extern const char http_hdr_Content_Encoding[]; +extern const char http_hdr_Content_Language[]; +extern const char http_hdr_Content_Length[]; +extern const char http_hdr_Content_Location[]; +extern const char http_hdr_Content_MD5[]; +extern const char http_hdr_Content_Range[]; +extern const char http_hdr_Content_Type[]; +extern const char http_hdr_Expires[]; +extern const char http_hdr_Last_Modified[]; + +/* general headers */ + +extern const char http_hdr_Cache_Control[]; +extern const char http_hdr_Connection[]; +extern const char http_hdr_Date[]; +extern const char http_hdr_Pragma[]; +extern const char http_hdr_Transfer_Encoding[]; +extern const char http_hdr_Update[]; +extern const char http_hdr_Trailer[]; +extern const char http_hdr_Via[]; + +/* request headers */ + +extern const char http_hdr_Accept[]; +extern const char http_hdr_Accept_Charset[]; +extern const char http_hdr_Accept_Encoding[]; +extern const char http_hdr_Accept_Language[]; +extern const char http_hdr_Authorization[]; +extern const char http_hdr_Expect[]; +extern const char http_hdr_From[]; +extern const char http_hdr_Host[]; +extern const char http_hdr_If_Modified_Since[]; +extern const char http_hdr_If_Match[]; +extern const char http_hdr_If_None_Match[]; +extern const char http_hdr_If_Range[]; +extern const char http_hdr_If_Unmodified_Since[]; +extern const char http_hdr_Max_Forwards[]; +extern const char http_hdr_Proxy_Authorization[]; +extern const char http_hdr_Range[]; +extern const char http_hdr_Referrer[]; +extern const char http_hdr_TE[]; +extern const char http_hdr_User_Agent[]; + +/* response headers */ + +extern const char http_hdr_Accept_Ranges[]; +extern const char http_hdr_Age[]; +extern const char http_hdr_ETag[]; +extern const char http_hdr_Location[]; +extern const char http_hdr_Retry_After[]; +extern const char http_hdr_Server[]; +extern const char http_hdr_Vary[]; +extern const char http_hdr_Warning[]; +extern const char http_hdr_WWW_Authenticate[]; + +/* Other headers */ + +extern const char http_hdr_Set_Cookie[]; + +/* WebDAV headers */ + +extern const char http_hdr_DAV[]; +extern const char http_hdr_Depth[]; +extern const char http_hdr_Destination[]; +extern const char http_hdr_If[]; +extern const char http_hdr_Lock_Token[]; +extern const char http_hdr_Overwrite[]; +extern const char http_hdr_Status_URI[]; +extern const char http_hdr_Timeout[]; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* GHTTP_CONSTANTS_H */ diff --git a/dynamic_library_test/http_request.c b/dynamic_library_test/http_request.c new file mode 100644 index 0000000..41a2f38 --- /dev/null +++ b/dynamic_library_test/http_request.c @@ -0,0 +1,235 @@ +#include +#include "http_request.h" + +ghttp_request *init_request() +{ + ghttp_request *request = NULL; + #ifdef DEBUG_MSG + openlog("http_request", LOG_PID, LOG_USER); + #endif + /* Allocate a new empty request object */ + request = ghttp_request_new(); + if (request == NULL) + { + LOG_PRINTF("Error: Func: %s Acclocate new empty request is NULL\n", __func__); + return NULL; + } + LOG_PRINTF("Info: Func: %s Acclocate new empty request is ok. \n", __func__); + return request; +} + +char *send_request11(ghttp_request *request, char *uri, int urilen, int mothod, char *path, int pathlen, char *body, int bodylen) +{ + //if(); + return NULL; +} + +void destory_request(ghttp_request *request) +{ + if(request != NULL) + { + ghttp_request_destroy(request); + } + LOG_PRINTF("Info: Func: %s Destroy request is ok. \n", __func__); + #ifdef DEBUG_MSG + closelog(); + #endif + return ; +} + +ghttp_request *send_request(char *uri, int urilen, int mothod, char *path, int pathlen, char *body, int bodylen, char *responsebuf, int *responselen) +{ + //char *uri = "http://www.solidot.org"; + /* This is the http request object */ + ghttp_request *request = NULL; + ghttp_status status; + char *buf = NULL, *allpath = NULL; + int bytes_read = 0, ret = 0, allpathlen = urilen + pathlen; + + if(uri == NULL || urilen == 0 ) + { + ret = -1; + LOG_PRINTF("Error: Func: %sAcclocate new empty request is NULL\n", __func__); + return request; + } + allpath = calloc(1, allpathlen + 1); + snprintf(allpath, allpathlen+1, "%s%s", uri, path); + LOG_PRINTF("Info: Func: %s allpath is %s allpath len is %d uri is %s. \n", __func__, allpath, pathlen, uri); + /* Allocate a new empty request object */ + request = ghttp_request_new(); + if (request == NULL) + { + ret = -1; + LOG_PRINTF("Error: Func: %s new request return error.\n", __func__); + return request; + } + else + { + LOG_PRINTF("Info: Func: %s Acclocate new empty request is ok. \n", __func__); + } + /* Set the URI for the request object */ + if(ghttp_set_uri(request, allpath) == -1) + { + ret = -1; + LOG_PRINTF("Error: Func: %s set uri return error.\n", __func__); + return request; + } + LOG_PRINTF("Info: Func: %s set uri is ok. \n", __func__); + /* Set the type for the request object */ + if(ghttp_set_type(request, (ghttp_type)mothod) == -1) + { + ret = -1; + LOG_PRINTF("Error: Func: %s set type return error.\n", __func__); + return request; + } + LOG_PRINTF("Info: Func: %s set type is ok. \n", __func__); + if(body != NULL && bodylen != 0) + { + LOG_PRINTF("Info: Func: %s set body is %s \n", __func__, body); + ret = ghttp_set_body(request,body, bodylen); + if (ret == -1) + { + LOG_PRINTF("Error: Func: %s set body return error.\n", __func__); + return request; + } + else + { + LOG_PRINTF("Info: Func: %s set body is ok. \n", __func__); + } + } + else + { + LOG_PRINTF("Info: Func: %s not set body.\n", __func__); + } + /* Prepare the connection */ + ghttp_prepare(request); + /* Process the request */ + status = ghttp_process(request); + if(status == ghttp_error) + { + ret = -1; + LOG_PRINTF("Error: Func: %s ghttp_process return error.\n", __func__); + return request; + } + /* OK, done */ + LOG_PRINTF("Info: Func: %s Status code -> %d\n", __func__, ghttp_status_code(request)); + /* Get the response for the request */ + buf = ghttp_get_body(request); + bytes_read = ghttp_get_body_len(request); + if(buf != NULL && bytes_read >= 0 ) + { + memcpy(responsebuf, buf, *responselen); + } + /* Get the response for the request*/ + *responselen = bytes_read; + LOG_PRINTF("Info: Func: %s get_body len is %d response is: %s \n", __func__, bytes_read, buf); + //printf("%s\n", buf); + return request; +} + +ghttp_request *send_request_async(char *uri, int urilen, int mothod, char *path, int pathlen, char *body, int bodylen, char *responsebuf, int *responselen) +{ + //char *uri = "http://www.solidot.org"; + /* This is the http request object */ + ghttp_request *request = NULL; + ghttp_status status; + char *buf = NULL, *allpath = NULL; + int bytes_read = 0, ret = 0, allpathlen = urilen + pathlen, get_response_len = 0; + + if(uri == NULL || urilen == 0 ) + { + ret = -1; + LOG_PRINTF("Error: Func: %sAcclocate new empty request is NULL\n", __func__); + return request; + } + allpath = calloc(1, allpathlen + 1); + snprintf(allpath, allpathlen+1, "%s%s", uri, path); + LOG_PRINTF("Info: Func: %s allpath is %s allpath len is %d uri is %s. \n", __func__, allpath, pathlen, uri); + /* Allocate a new empty request object */ + request = ghttp_request_new(); + if (request == NULL) + { + ret = -1; + LOG_PRINTF("Error: Func: %s new request return error.\n", __func__); + return request; + } + else + { + LOG_PRINTF("Info: Func: %s Acclocate new empty request is ok. \n", __func__); + } + /* Set the URI for the request object */ + if(ghttp_set_uri(request, allpath) == -1) + { + ret = -1; + LOG_PRINTF("Error: Func: %s set uri return error.\n", __func__); + return request; + } + LOG_PRINTF("Info: Func: %s set uri is ok. \n", __func__); + /* Set the type for the request object */ + if(ghttp_set_type(request, (ghttp_type)mothod) == -1) + { + ret = -1; + LOG_PRINTF("Error: Func: %s set type return error.\n", __func__); + return request; + } + LOG_PRINTF("Info: Func: %s set type is ok. \n", __func__); + if(body != NULL && bodylen != 0) + { + LOG_PRINTF("Info: Func: %s set body is %s \n", __func__, body); + ret = ghttp_set_body(request,body, bodylen); + if (ret == -1) + { + LOG_PRINTF("Error: Func: %s set body return error.\n", __func__); + return request; + } + else + { + LOG_PRINTF("Info: Func: %s set body is ok. \n", __func__); + } + } + else + { + LOG_PRINTF("Info: Func: %s not set body.\n", __func__); + } + /* Set async request */ + ghttp_set_sync(request, ghttp_async); + /* Prepare the connection */ + ghttp_prepare(request); + while(1) + { + /* Process the request */ + status = ghttp_process(request); + if(status == ghttp_error) + { + ret = -1; + LOG_PRINTF("Error: Func: %s ghttp_process return error.\n", __func__); + break; + } + /* OK, done */ + LOG_PRINTF("Info: Func: %s Status code -> %d\n", __func__, ghttp_status_code(request)); + /* Get the response for the request */ + buf = ghttp_get_body(request); + /* Get the response for the request*/ + bytes_read = ghttp_get_body_len(request); + get_response_len += bytes_read; + if(get_response_len >= *responselen) + { + LOG_PRINTF("Info: Func: %s get_response_len is gt responselen ---break!!\n", __func__); + break; + } + else + { + memcpy(responsebuf, buf, bytes_read); + responsebuf = responsebuf + bytes_read; + } + *responselen = bytes_read; + LOG_PRINTF("Info: Func: %s get_body len is %d response is: %s \n", __func__, bytes_read, buf); + if (status == ghttp_done) + { + break; + } + } + //printf("%s\n", buf); + return request; +} + diff --git a/dynamic_library_test/http_request.h b/dynamic_library_test/http_request.h new file mode 100644 index 0000000..d510062 --- /dev/null +++ b/dynamic_library_test/http_request.h @@ -0,0 +1,21 @@ +#include +#include +#include +#include "ghttp.h" + +#ifdef DEBUG_MSG +#define LOG_PRINTF(fmt, arg...) syslog(LOG_DEBUG, fmt, arg) +#else +#define LOG_PRINTF(fmt, arg...) +#endif + +//ghttp_request *init_request(); + +//char *send_request11(ghttp_request *request, char *uri, int urilen, int mothod, char *path, int pathlen, char *body, int bodylen); + +//mothod的值请参看Readme的枚举进行对应的整数值,uri和urilen必须有值且正确,若无path则path为NULL,若无body,body为NULL,responsebuf是想要接收的回应的空间,responselen是想要得到回应的长度 +ghttp_request * send_request(char *uri, int urilen, int mothod, char *path, int pathlen, char *body, int bodylen, char *responsebuf, int *responselen); + +//对send_request产生的ghttp_request的资源进行释放` +void destory_request(ghttp_request *request); + diff --git a/dynamic_library_test/include/ghttp.h b/dynamic_library_test/include/ghttp.h new file mode 100755 index 0000000..08a68f6 --- /dev/null +++ b/dynamic_library_test/include/ghttp.h @@ -0,0 +1,263 @@ +/* + * ghttp.h -- A public interface to common http functions + * Created: Christopher Blizzard , 21-Aug-1998 + * + * Copyright (C) 1998 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GHTTP_H +#define GHTTP_H + +#include "ghttp_constants.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct _ghttp_request ghttp_request; + +typedef enum ghttp_type_tag +{ + ghttp_type_get = 0, + ghttp_type_options, + ghttp_type_head, + ghttp_type_post, + ghttp_type_put, + ghttp_type_delete, + ghttp_type_trace, + ghttp_type_connect, + ghttp_type_propfind, + ghttp_type_proppatch, + ghttp_type_mkcol, + ghttp_type_copy, + ghttp_type_move, + ghttp_type_lock, + ghttp_type_unlock +} ghttp_type; + +typedef enum ghttp_sync_mode_tag +{ + ghttp_sync = 0, + ghttp_async +} ghttp_sync_mode; + +typedef enum ghttp_status_tag +{ + ghttp_error = -1, + ghttp_not_done, + ghttp_done +} ghttp_status; + +typedef enum ghttp_proc_tag +{ + ghttp_proc_none = 0, + ghttp_proc_request, + ghttp_proc_response_hdrs, + ghttp_proc_response +} ghttp_proc; + +typedef struct ghttp_current_status_tag +{ + ghttp_proc proc; /* what's it doing? */ + int bytes_read; /* how many bytes have been read? */ + int bytes_total; /* how many total */ +} ghttp_current_status; + +/* create a new request object */ +ghttp_request * +ghttp_request_new(void); + +/* delete a current request object */ +void +ghttp_request_destroy(ghttp_request *a_request); + +/* Validate a uri + * This will return -1 if a uri is invalid + */ +int +ghttp_uri_validate(char *a_uri); + +/* Set a uri in a request + * This will return -1 if the uri is invalid + */ + +int +ghttp_set_uri(ghttp_request *a_request, char *a_uri); + +/* Set a proxy for a request + * This will return -1 if the uri is invalid + */ + +int +ghttp_set_proxy(ghttp_request *a_request, char *a_uri); + +/* Set a request type + * This will return -1 if the request type is invalid or + * unsupported + */ + +int +ghttp_set_type(ghttp_request *a_request, ghttp_type a_type); + +/* Set the body. + * This will return -1 if the request type doesn't support it + */ + +int +ghttp_set_body(ghttp_request *a_request, char *a_body, int a_len); + +/* Set whether or not you want to use sync or async mode. + */ + +int +ghttp_set_sync(ghttp_request *a_request, + ghttp_sync_mode a_mode); + +/* Prepare a request. + * Call this before trying to process a request or if you change the + * uri. + */ + +int +ghttp_prepare(ghttp_request *a_request); + +/* Set the chunk size + * You might want to do this to optimize for different connection speeds. + */ + +void +ghttp_set_chunksize(ghttp_request *a_request, int a_size); + +/* Set a random request header + */ + +void +ghttp_set_header(ghttp_request *a_request, + const char *a_hdr, const char *a_val); + +/* Process a request + */ + +ghttp_status +ghttp_process(ghttp_request *a_request); + +/* Get the status of a request + */ + +ghttp_current_status +ghttp_get_status(ghttp_request *a_request); + +/* Flush the received data (so far) into the response body. This is + * useful for asynchronous requests with large responses: you can + * periodically flush the response buffer and parse the data that's + * arrived so far. + */ + +void +ghttp_flush_response_buffer(ghttp_request *a_request); + +/* Get the value of a random response header + */ + +const char * +ghttp_get_header(ghttp_request *a_request, + const char *a_hdr); + +/* Get the list of headers that were returned in the response. You + must free the returned string values. This function will return 0 + on success, -1 on some kind of error. */ +int +ghttp_get_header_names(ghttp_request *a_request, + char ***a_hdrs, int *a_num_hdrs); + +/* Abort a currently running request. */ +int +ghttp_close(ghttp_request *a_request); + +/* Clean a request + */ +void +ghttp_clean(ghttp_request *a_request); + +/* Get the socket associated with a particular connection + */ + +int +ghttp_get_socket(ghttp_request *a_request); + +/* get the return entity body + */ + +char * +ghttp_get_body(ghttp_request *a_request); + +/* get the returned length + */ + +int +ghttp_get_body_len(ghttp_request *a_request); + +/* Get an error message for a request that has failed. + */ + +const char * +ghttp_get_error(ghttp_request *a_request); + +/* Parse a date string that is one of the standard + * date formats + */ + +time_t +ghttp_parse_date(char *a_date); + +/* Return the status code. + */ + +int +ghttp_status_code(ghttp_request *a_request); + +/* Return the reason phrase. + */ + +const char * +ghttp_reason_phrase(ghttp_request *a_request); + +/* Set your username/password pair + */ + +int +ghttp_set_authinfo(ghttp_request *a_request, + const char *a_user, + const char *a_pass); + + + /* Set your username/password pair for proxy + */ + +int +ghttp_set_proxy_authinfo(ghttp_request *a_request, + const char *a_user, + const char *a_pass); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* GHTTP_H */ diff --git a/dynamic_library_test/include/ghttp_constants.h b/dynamic_library_test/include/ghttp_constants.h new file mode 100755 index 0000000..7e1d07a --- /dev/null +++ b/dynamic_library_test/include/ghttp_constants.h @@ -0,0 +1,105 @@ +/* + * ghttp_constants.h -- definitions for char constants that people + * might want to use + * Created: Christopher Blizzard + * + * Copyright (C) 1998 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GHTTP_CONSTANTS_H +#define GHTTP_CONSTANTS_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +extern const char http_hdr_Allow[]; +extern const char http_hdr_Content_Encoding[]; +extern const char http_hdr_Content_Language[]; +extern const char http_hdr_Content_Length[]; +extern const char http_hdr_Content_Location[]; +extern const char http_hdr_Content_MD5[]; +extern const char http_hdr_Content_Range[]; +extern const char http_hdr_Content_Type[]; +extern const char http_hdr_Expires[]; +extern const char http_hdr_Last_Modified[]; + +/* general headers */ + +extern const char http_hdr_Cache_Control[]; +extern const char http_hdr_Connection[]; +extern const char http_hdr_Date[]; +extern const char http_hdr_Pragma[]; +extern const char http_hdr_Transfer_Encoding[]; +extern const char http_hdr_Update[]; +extern const char http_hdr_Trailer[]; +extern const char http_hdr_Via[]; + +/* request headers */ + +extern const char http_hdr_Accept[]; +extern const char http_hdr_Accept_Charset[]; +extern const char http_hdr_Accept_Encoding[]; +extern const char http_hdr_Accept_Language[]; +extern const char http_hdr_Authorization[]; +extern const char http_hdr_Expect[]; +extern const char http_hdr_From[]; +extern const char http_hdr_Host[]; +extern const char http_hdr_If_Modified_Since[]; +extern const char http_hdr_If_Match[]; +extern const char http_hdr_If_None_Match[]; +extern const char http_hdr_If_Range[]; +extern const char http_hdr_If_Unmodified_Since[]; +extern const char http_hdr_Max_Forwards[]; +extern const char http_hdr_Proxy_Authorization[]; +extern const char http_hdr_Range[]; +extern const char http_hdr_Referrer[]; +extern const char http_hdr_TE[]; +extern const char http_hdr_User_Agent[]; + +/* response headers */ + +extern const char http_hdr_Accept_Ranges[]; +extern const char http_hdr_Age[]; +extern const char http_hdr_ETag[]; +extern const char http_hdr_Location[]; +extern const char http_hdr_Retry_After[]; +extern const char http_hdr_Server[]; +extern const char http_hdr_Vary[]; +extern const char http_hdr_Warning[]; +extern const char http_hdr_WWW_Authenticate[]; + +/* Other headers */ + +extern const char http_hdr_Set_Cookie[]; + +/* WebDAV headers */ + +extern const char http_hdr_DAV[]; +extern const char http_hdr_Depth[]; +extern const char http_hdr_Destination[]; +extern const char http_hdr_If[]; +extern const char http_hdr_Lock_Token[]; +extern const char http_hdr_Overwrite[]; +extern const char http_hdr_Status_URI[]; +extern const char http_hdr_Timeout[]; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* GHTTP_CONSTANTS_H */ diff --git a/dynamic_library_test/libghttp.a b/dynamic_library_test/libghttp.a new file mode 100644 index 0000000..9632431 Binary files /dev/null and b/dynamic_library_test/libghttp.a differ diff --git a/dynamic_library_test/request_send.so b/dynamic_library_test/request_send.so new file mode 100755 index 0000000..ad3e04a Binary files /dev/null and b/dynamic_library_test/request_send.so differ diff --git a/dynamic_library_test/test.c b/dynamic_library_test/test.c new file mode 100644 index 0000000..5974dfb --- /dev/null +++ b/dynamic_library_test/test.c @@ -0,0 +1,15 @@ +#include +#include +#include "http_request.h" + +int main() +{ + char *uri = "http://www.solidot.org"; + char response[80*1024] = {0}; + int responselen = 80*1024; + ghttp_request *testreq = send_request(uri, strlen(uri), 0, NULL, 0, NULL, 0, response, &responselen); + int lenn = strlen(uri); + printf("response is %s len is %d lenn is %d\n", response, responselen, lenn); + destory_request(testreq); + return 0; +} diff --git a/ghttp.h b/ghttp.h index 4ee20e1..08a68f6 100755 --- a/ghttp.h +++ b/ghttp.h @@ -22,7 +22,7 @@ #ifndef GHTTP_H #define GHTTP_H -#include +#include "ghttp_constants.h" #include #ifdef __cplusplus diff --git a/lib/include/ghttp.h b/lib/include/ghttp.h new file mode 100755 index 0000000..08a68f6 --- /dev/null +++ b/lib/include/ghttp.h @@ -0,0 +1,263 @@ +/* + * ghttp.h -- A public interface to common http functions + * Created: Christopher Blizzard , 21-Aug-1998 + * + * Copyright (C) 1998 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GHTTP_H +#define GHTTP_H + +#include "ghttp_constants.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct _ghttp_request ghttp_request; + +typedef enum ghttp_type_tag +{ + ghttp_type_get = 0, + ghttp_type_options, + ghttp_type_head, + ghttp_type_post, + ghttp_type_put, + ghttp_type_delete, + ghttp_type_trace, + ghttp_type_connect, + ghttp_type_propfind, + ghttp_type_proppatch, + ghttp_type_mkcol, + ghttp_type_copy, + ghttp_type_move, + ghttp_type_lock, + ghttp_type_unlock +} ghttp_type; + +typedef enum ghttp_sync_mode_tag +{ + ghttp_sync = 0, + ghttp_async +} ghttp_sync_mode; + +typedef enum ghttp_status_tag +{ + ghttp_error = -1, + ghttp_not_done, + ghttp_done +} ghttp_status; + +typedef enum ghttp_proc_tag +{ + ghttp_proc_none = 0, + ghttp_proc_request, + ghttp_proc_response_hdrs, + ghttp_proc_response +} ghttp_proc; + +typedef struct ghttp_current_status_tag +{ + ghttp_proc proc; /* what's it doing? */ + int bytes_read; /* how many bytes have been read? */ + int bytes_total; /* how many total */ +} ghttp_current_status; + +/* create a new request object */ +ghttp_request * +ghttp_request_new(void); + +/* delete a current request object */ +void +ghttp_request_destroy(ghttp_request *a_request); + +/* Validate a uri + * This will return -1 if a uri is invalid + */ +int +ghttp_uri_validate(char *a_uri); + +/* Set a uri in a request + * This will return -1 if the uri is invalid + */ + +int +ghttp_set_uri(ghttp_request *a_request, char *a_uri); + +/* Set a proxy for a request + * This will return -1 if the uri is invalid + */ + +int +ghttp_set_proxy(ghttp_request *a_request, char *a_uri); + +/* Set a request type + * This will return -1 if the request type is invalid or + * unsupported + */ + +int +ghttp_set_type(ghttp_request *a_request, ghttp_type a_type); + +/* Set the body. + * This will return -1 if the request type doesn't support it + */ + +int +ghttp_set_body(ghttp_request *a_request, char *a_body, int a_len); + +/* Set whether or not you want to use sync or async mode. + */ + +int +ghttp_set_sync(ghttp_request *a_request, + ghttp_sync_mode a_mode); + +/* Prepare a request. + * Call this before trying to process a request or if you change the + * uri. + */ + +int +ghttp_prepare(ghttp_request *a_request); + +/* Set the chunk size + * You might want to do this to optimize for different connection speeds. + */ + +void +ghttp_set_chunksize(ghttp_request *a_request, int a_size); + +/* Set a random request header + */ + +void +ghttp_set_header(ghttp_request *a_request, + const char *a_hdr, const char *a_val); + +/* Process a request + */ + +ghttp_status +ghttp_process(ghttp_request *a_request); + +/* Get the status of a request + */ + +ghttp_current_status +ghttp_get_status(ghttp_request *a_request); + +/* Flush the received data (so far) into the response body. This is + * useful for asynchronous requests with large responses: you can + * periodically flush the response buffer and parse the data that's + * arrived so far. + */ + +void +ghttp_flush_response_buffer(ghttp_request *a_request); + +/* Get the value of a random response header + */ + +const char * +ghttp_get_header(ghttp_request *a_request, + const char *a_hdr); + +/* Get the list of headers that were returned in the response. You + must free the returned string values. This function will return 0 + on success, -1 on some kind of error. */ +int +ghttp_get_header_names(ghttp_request *a_request, + char ***a_hdrs, int *a_num_hdrs); + +/* Abort a currently running request. */ +int +ghttp_close(ghttp_request *a_request); + +/* Clean a request + */ +void +ghttp_clean(ghttp_request *a_request); + +/* Get the socket associated with a particular connection + */ + +int +ghttp_get_socket(ghttp_request *a_request); + +/* get the return entity body + */ + +char * +ghttp_get_body(ghttp_request *a_request); + +/* get the returned length + */ + +int +ghttp_get_body_len(ghttp_request *a_request); + +/* Get an error message for a request that has failed. + */ + +const char * +ghttp_get_error(ghttp_request *a_request); + +/* Parse a date string that is one of the standard + * date formats + */ + +time_t +ghttp_parse_date(char *a_date); + +/* Return the status code. + */ + +int +ghttp_status_code(ghttp_request *a_request); + +/* Return the reason phrase. + */ + +const char * +ghttp_reason_phrase(ghttp_request *a_request); + +/* Set your username/password pair + */ + +int +ghttp_set_authinfo(ghttp_request *a_request, + const char *a_user, + const char *a_pass); + + + /* Set your username/password pair for proxy + */ + +int +ghttp_set_proxy_authinfo(ghttp_request *a_request, + const char *a_user, + const char *a_pass); + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + + +#endif /* GHTTP_H */ diff --git a/lib/include/ghttp_constants.h b/lib/include/ghttp_constants.h new file mode 100755 index 0000000..7e1d07a --- /dev/null +++ b/lib/include/ghttp_constants.h @@ -0,0 +1,105 @@ +/* + * ghttp_constants.h -- definitions for char constants that people + * might want to use + * Created: Christopher Blizzard + * + * Copyright (C) 1998 Free Software Foundation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef GHTTP_CONSTANTS_H +#define GHTTP_CONSTANTS_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +extern const char http_hdr_Allow[]; +extern const char http_hdr_Content_Encoding[]; +extern const char http_hdr_Content_Language[]; +extern const char http_hdr_Content_Length[]; +extern const char http_hdr_Content_Location[]; +extern const char http_hdr_Content_MD5[]; +extern const char http_hdr_Content_Range[]; +extern const char http_hdr_Content_Type[]; +extern const char http_hdr_Expires[]; +extern const char http_hdr_Last_Modified[]; + +/* general headers */ + +extern const char http_hdr_Cache_Control[]; +extern const char http_hdr_Connection[]; +extern const char http_hdr_Date[]; +extern const char http_hdr_Pragma[]; +extern const char http_hdr_Transfer_Encoding[]; +extern const char http_hdr_Update[]; +extern const char http_hdr_Trailer[]; +extern const char http_hdr_Via[]; + +/* request headers */ + +extern const char http_hdr_Accept[]; +extern const char http_hdr_Accept_Charset[]; +extern const char http_hdr_Accept_Encoding[]; +extern const char http_hdr_Accept_Language[]; +extern const char http_hdr_Authorization[]; +extern const char http_hdr_Expect[]; +extern const char http_hdr_From[]; +extern const char http_hdr_Host[]; +extern const char http_hdr_If_Modified_Since[]; +extern const char http_hdr_If_Match[]; +extern const char http_hdr_If_None_Match[]; +extern const char http_hdr_If_Range[]; +extern const char http_hdr_If_Unmodified_Since[]; +extern const char http_hdr_Max_Forwards[]; +extern const char http_hdr_Proxy_Authorization[]; +extern const char http_hdr_Range[]; +extern const char http_hdr_Referrer[]; +extern const char http_hdr_TE[]; +extern const char http_hdr_User_Agent[]; + +/* response headers */ + +extern const char http_hdr_Accept_Ranges[]; +extern const char http_hdr_Age[]; +extern const char http_hdr_ETag[]; +extern const char http_hdr_Location[]; +extern const char http_hdr_Retry_After[]; +extern const char http_hdr_Server[]; +extern const char http_hdr_Vary[]; +extern const char http_hdr_Warning[]; +extern const char http_hdr_WWW_Authenticate[]; + +/* Other headers */ + +extern const char http_hdr_Set_Cookie[]; + +/* WebDAV headers */ + +extern const char http_hdr_DAV[]; +extern const char http_hdr_Depth[]; +extern const char http_hdr_Destination[]; +extern const char http_hdr_If[]; +extern const char http_hdr_Lock_Token[]; +extern const char http_hdr_Overwrite[]; +extern const char http_hdr_Status_URI[]; +extern const char http_hdr_Timeout[]; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* GHTTP_CONSTANTS_H */ diff --git a/lib/libghttp.a b/lib/libghttp.a new file mode 100644 index 0000000..9632431 Binary files /dev/null and b/lib/libghttp.a differ diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..f527111 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,5 @@ +all: + gcc http_request.c -o http_request -I../lib/include/ -L../lib/ -lghttp + +clean: + rm http_request diff --git a/test/http_request b/test/http_request new file mode 100755 index 0000000..34f0618 Binary files /dev/null and b/test/http_request differ diff --git a/test/http_request.c b/test/http_request.c new file mode 100644 index 0000000..9315446 --- /dev/null +++ b/test/http_request.c @@ -0,0 +1,40 @@ +#include +#include +#include "ghttp.h" + +//A simple example from doc/ghttp.html +int main() +{ + char *uri = "http://www.solidot.org"; + /* This is the http request object */ + ghttp_request *request = NULL; + ghttp_status status; + char *buf = NULL; + int bytes_read = 0; + + /* Allocate a new empty request object */ + request = ghttp_request_new(); + /* Set the URI for the request object */ + if(ghttp_set_uri(request, uri) == -1) + exit(-1); + /* Set the type for the request object */ + if(ghttp_set_type(request, ghttp_type_get) == -1) + exit(-1); + /* Prepare the connection */ + ghttp_prepare(request); + /* Process the request */ + status = ghttp_process(request); + if(status == ghttp_error) + exit(-1); + /* OK, done */ + printf("Status code -> %d\n", ghttp_status_code(request)); + /* Get the response for the request */ + buf = ghttp_get_body(request); + /* Get the response for the request*/ + bytes_read = ghttp_get_body_len(request); + printf("%s\n", buf); + /* Destroy the request. This closes any file descriptors that may be open and will free any memory associated with the request. */ + ghttp_request_destroy(request); + return 0; +} +