static int init(const char *config_file,const char *orderid_file) { int fd_order; request = ghttp_request_new(); my_object = json_object_from_file(config_file); machine_id = json_object_get_int(json_object_object_get(my_object,MACHINE_ID)); info_version = json_object_get_int(json_object_object_get(my_object,INFO_VERSION)); format_qrcode = json_object_get_string(json_object_object_get(my_object,ENCODE_FORMAT)); fd_order=open(orderid_file,O_RDWR); if(fd_order==-1) { int order_id=0; fd_order=open(orderid_file,O_RDWR|O_CREAT,0666); write(fd_order,(void*)&order_id,sizeof(unsigned int)); lseek(fd_order,0,SEEK_SET); } p_order_id=(unsigned int*)mmap(NULL,sizeof(unsigned int),PROT_READ|PROT_WRITE,MAP_SHARED,fd_order,0); if(p_order_id==-1) return -3; ghttp_set_uri(request, json_object_get_string(json_object_object_get(my_object,HOST))); ghttp_prepare(request); ghttp_set_sync(request, ghttp_async); ghttp_set_type(request, ghttp_type_post); ghttp_set_header(request, http_hdr_Connection, "keep-alive"); http_request(REQ_REGISTER); while(http_response_process((void*)config_file)==-1); ghttp_flush_response_buffer(request); return 0; }
int ghttp_work_httpheader(ghttp_request *request, ghttp_type request_type) { int ret = 0; ghttp_status req_status; ghttp_proc req_proc; if(!request) return -1; ghttp_set_type(request, request_type); ghttp_set_sync(request, ghttp_async); if( ghttp_prepare(request) < 0 ){ ret = -3; goto END; } do { req_status = ghttp_process(request); if( req_status == ghttp_error ){ ghttpDebug("%s \n", ghttp_get_error(request)); ret = -3; goto END; } else { req_proc = ghttp_get_proc(request); if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done ){ break; } } }while (req_status == ghttp_not_done); END: return ret; }
char *send_http_request(ghttp_request *req, char *uri) { #define MALLOC_SIZE 5120 ghttp_status req_status; unsigned long rec_bytes_total = 0; unsigned long buffer_size = 0; unsigned long rec_bytes_current = 0; char *buffer = (char *)malloc(sizeof(char) * MALLOC_SIZE); if(!buffer) bail("malloc space error"); else { memset(buffer, 0, MALLOC_SIZE); buffer_size = MALLOC_SIZE; } if(ghttp_set_uri(req, uri) < 0) bail("ghttp_set_uri"); if(ghttp_prepare(req) < 0) bail("ghttp_prepare"); if(ghttp_set_type(req, ghttp_type_get) == -1) bail("ghttp_set_type"); if(ghttp_set_sync(req, ghttp_async) < 0) bail("ghttp_set_sync"); do { status(req, "conn"); req_status = ghttp_process(req); if(req_status == ghttp_error) { fprintf(stderr, "ghttp_process: %s\n", ghttp_get_error(req)); return ""; } if(req_status != ghttp_error && ghttp_get_body_len(req) > 0) { rec_bytes_current = ghttp_get_body_len(req); rec_bytes_total += rec_bytes_current; while(rec_bytes_total > buffer_size) { buffer = (char *)realloc(buffer, sizeof(char)*(buffer_size + MALLOC_SIZE)); if(!buffer) bail("realloc error"); buffer_size += MALLOC_SIZE; } strncat(buffer, ghttp_get_body(req), rec_bytes_current); buffer[rec_bytes_total] = '\0'; ghttp_flush_response_buffer(req); } } while(req_status == ghttp_not_done); ghttp_clean(req); return buffer; }
ghttp_request *ghttp_request_new2(ghttp_type action, char *url, ghttp_sync_mode mode) { ghttp_request *request = NULL; if(!url) return NULL; request = ghttp_request_new(); if(!request) return NULL; if( ghttp_set_uri(request, url) < 0) goto ERR; ghttp_set_type(request, action); ghttp_set_sync(request, mode); return request; ERR: ghttp_request_destroy(request); return NULL; }
static ghttp_request *ghttp_request_redirect(ghttp_request *cur_request, char *new_url) { ghttp_request *request = NULL; if (!cur_request || !cur_request->req || !cur_request->conn) return NULL; request = ghttp_request_new(); if(!request) return NULL; if( ghttp_set_uri(request, new_url) < 0) goto ERR; ghttp_set_type(request, cur_request->req->type); ghttp_set_sync(request, cur_request->conn->sync); return request; ERR: ghttp_request_destroy(request); return NULL; }
int ghttp_download_file(char *path, char *url) { ghttp_request *request = NULL; FILE * pFile=NULL; char *buf=NULL; int ret = 0; ghttp_status req_status; ghttp_proc req_proc; int bytes_read=0,recvbytes=0; int status_code=0; char *redirect = NULL; #if GHTTP_DEBUG char *tmp_pchar = NULL; #endif request = ghttp_request_new(); if( ghttp_set_uri(request, url) < 0 ){ ghttpDebug("invalid url: %s \n", url); ret = -1; goto END; } if(!path) path = ghttp_get_resource_name(request); if(!path) path = "httpget.html"; pFile = fopen ( path , "wb" ); if(pFile == NULL){ ghttpDebug("error: %s [%s]\n", strerror(errno), path); ret = -2; goto END; } ghttpDebug("host: %s \n", ghttp_get_host(request)); if( ghttp_set_type(request, ghttp_type_get) < 0 ){ ret = -3; goto END; } if (ghttp_set_sync(request, ghttp_async) < 0){ ret = -3; goto END; } if( ghttp_prepare(request) < 0 ){ ret = -3; goto END; } do { req_status = ghttp_process(request); if( req_status == ghttp_error ){ ghttpDebug("%s \n", ghttp_get_error(request)); ret = -4; goto END; } else { if( req_status == ghttp_done ) { status_code = ghttp_status_code(request); if(status_code != 200){ fclose(pFile); pFile = NULL; break; } } req_proc = ghttp_get_proc(request); if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done ) { #if GHTTP_DEBUG if( !tmp_pchar ) { tmp_pchar = (char *)ghttp_get_header(request, "Content-Length"); ghttpDebug("Content-Length: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Transfer-Encoding"); ghttpDebug("Transfer-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Content-Encoding"); ghttpDebug("Content-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)1; ghttpDebug("recvbytes: "); } #endif ghttp_flush_response_buffer(request); if(ghttp_get_body_len(request) > 0) { buf = ghttp_get_body(request); bytes_read = ghttp_get_body_len(request); recvbytes += bytes_read; if(buf) fwrite(buf,bytes_read,1,pFile); } ghttpDebug("%d", recvbytes); } } }while (req_status == ghttp_not_done); //ret = status_code; switch(status_code) { case 200: default: break; case 302: buf = (char *)ghttp_get_header(request, "Location"); if(buf){ redirect = (char *)malloc(strlen(buf)+1); if(redirect == NULL){ ret = -1; goto END; } strcpy(redirect, buf); } break; } END: ghttp_request_destroy(request); if(pFile) fclose(pFile); if(redirect){ ghttpDebug("redirect: %s \n", redirect); ret = ghttp_download_file(path, redirect); free(redirect); } return ret; }
int ghttp_post_work(ghttp_request *request, struct ghttp_result *result, struct ghttp_post_data *data) { int ret = 0; ghttp_status req_status; ghttp_proc req_proc; int status_code = 0; // ghttp_request *redirect_request; // char *redirect = NULL, *buf = NULL; #if GHTTP_DEBUG char *tmp_pchar = NULL; #endif if(!request || !data) return -1; ghttp_set_type(request, ghttp_type_post); ghttp_set_sync(request, ghttp_async); if( ghttp_prepare(request) < 0 ){ ret = -3; goto END; } do { req_status = ghttp_process(request); if( req_status == ghttp_error ){ ghttpDebug("%s \n", ghttp_get_error(request)); ret = -3; goto END; } else { if(req_status == ghttp_next ){ data->post_data_func(request, data); } if( req_status == ghttp_done ) { status_code = ghttp_status_code(request); if(status_code != 200){ ghttp_result_recv_finish(request, result); break; } } req_proc = ghttp_get_proc(request); if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done ) { #if GHTTP_DEBUG if( !tmp_pchar ) { tmp_pchar = (char *)ghttp_get_header(request, "Content-Length"); ghttpDebug("Content-Length: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Transfer-Encoding"); ghttpDebug("Transfer-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Content-Encoding"); ghttpDebug("Content-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Content-Type"); ghttpDebug("Content-Type: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)1; } #endif ghttp_result_recv(request, result); } } }while (req_status == ghttp_not_done); END: ghttpDebug("http_code: %d \n", status_code); if(ret == 0 && (status_code < 200 || status_code >= 300)) ret = -3; if(result) result->http_code = status_code; ghttp_result_recv_finish(request, result); ghttp_request_destroy(request); return ret; }
int ghttp_get_work(ghttp_request *request, struct ghttp_result *result) { int ret = 0; ghttp_status req_status; ghttp_proc req_proc; int status_code = 0; ghttp_request *redirect_request; char *redirect = NULL, *buf = NULL; #if GHTTP_DEBUG char *tmp_pchar = NULL; #endif if(!request) return -1; ghttp_set_type(request, ghttp_type_get); ghttp_set_sync(request, ghttp_async); if( ghttp_prepare(request) < 0 ){ ret = -3; goto END; } do { req_status = ghttp_process(request); if( req_status == ghttp_error ){ ghttpDebug("%s \n", ghttp_get_error(request)); ret = -3; goto END; } else { if( req_status == ghttp_done ) { status_code = ghttp_status_code(request); if(status_code != 200){ ghttp_result_recv_finish(request, result); break; } } req_proc = ghttp_get_proc(request); if( req_proc == ghttp_proc_response || req_proc == ghttp_proc_done ) { #if GHTTP_DEBUG if( !tmp_pchar ) { tmp_pchar = (char *)ghttp_get_header(request, "Content-Length"); ghttpDebug("Content-Length: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Transfer-Encoding"); ghttpDebug("Transfer-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Content-Encoding"); ghttpDebug("Content-Encoding: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)ghttp_get_header(request, "Content-Type"); ghttpDebug("Content-Type: %s \n", tmp_pchar ? tmp_pchar : "null"); tmp_pchar = (char *)1; } #endif ghttp_result_recv(request, result); } } }while (req_status == ghttp_not_done); switch(status_code) { case 200: default: break; case 302: buf = (char *)ghttp_get_header(request, "Location"); if(buf){ redirect = (char *)malloc(strlen(buf)+1); if(redirect == NULL){ ret = -2; goto END; } strcpy(redirect, buf); } break; } END: if(result) result->http_code = status_code; ghttp_result_recv_finish(request, result); if(redirect){ ghttpDebug("redirect: %s \n", redirect); redirect_request = ghttp_request_redirect(request, redirect); ghttp_request_destroy(request); free(redirect); ret = ghttp_get_work(redirect_request, ghttp_result_clean(result)); } else ghttp_request_destroy(request); return ret; }
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; }
char *send_http_request(ghttp_request *req, char *uri) { #define malloc_size 5120 ghttp_status req_status; unsigned long rec_bytes_total = 0; unsigned long buffer_size = 0; unsigned long rec_bytes_current = 0; char *buffer = (char *)malloc(sizeof(char) * malloc_size); if(!buffer) { printf("malloc space error\n"); return NULL; } else { memset(buffer, 0, malloc_size); buffer_size = malloc_size; } if(ghttp_set_uri(req, uri) < 0) { printf("ghttp_set_uri\n"); return NULL; } ghttp_set_header(req,http_hdr_Connection,"close"); char timeout_str[10]; sprintf(timeout_str,"%d",5000); ghttp_set_header(req,http_hdr_Timeout,timeout_str); if(ghttp_prepare(req) < 0) { printf("ghttp_prepare\n"); return NULL; } if(ghttp_set_type(req, ghttp_type_get) == -1) { printf("ghttp_set_type\n"); return NULL; } if(ghttp_set_sync(req, ghttp_async) < 0) { printf("ghttp_set_sync\n"); return NULL; } do { status(req, "conn"); req_status = ghttp_process(req); if(req_status == ghttp_error) { fprintf(stderr, "ghttp_process: %s\n", ghttp_get_error(req)); return ""; } if(req_status != ghttp_error && ghttp_get_body_len(req) > 0) { rec_bytes_current = ghttp_get_body_len(req); rec_bytes_total += rec_bytes_current; while(rec_bytes_total > buffer_size) { buffer = (char *)realloc(buffer, buffer_size + malloc_size); if(!buffer) { printf("realloc error\n"); return NULL; } buffer_size += malloc_size; } strncat(buffer, ghttp_get_body(req), rec_bytes_current); buffer[rec_bytes_total] = '\0'; ghttp_flush_response_buffer(req); } } while(req_status == ghttp_not_done); ghttp_clean(req); return buffer; }