Exemplo n.º 1
0
static int ghttp_result_save_file(ghttp_request *request, struct ghttp_result *result)
{
	char *buf = NULL;
	int bytes_read = 0;
	
	if(!request || !result)
		return -1;

	if(result->finish){
		fclose(result->fp);
		result->fp = NULL;
		return 0;
	}

	ghttp_flush_response_buffer(request);
	if(ghttp_get_body_len(request) > 0)
	{
		buf = ghttp_get_body(request);
		bytes_read = ghttp_get_body_len(request);
		
		if(buf){
			if( fwrite(buf, bytes_read, 1, result->fp) == bytes_read)
				result->bytes += bytes_read;
		}
	}

	return 0;
}
Exemplo n.º 2
0
static int ghttp_result_save_buff(ghttp_request *request, struct ghttp_result *result)
{
	char *buf = NULL;
	int bytes_read = 0;
	int tmp = 0;
	
	if(!request || !result)
		return -1;

	if(result->finish){
		return 0;
	}

	ghttp_flush_response_buffer(request);
	if(ghttp_get_body_len(request) > 0)
	{
		buf = ghttp_get_body(request);
		bytes_read = ghttp_get_body_len(request);
		//ghttpDebug("buf: %p, bytes_read: %d \n", buf, bytes_read);
		if(buf){
			tmp = result->bytes + bytes_read;
			if(tmp >= result->buff_size){
				bytes_read = tmp - result->bytes;
				result->finish = 1;
			}
			memcpy(result->buff + result->bytes, buf, bytes_read);
			result->bytes += bytes_read;
		}
	}

	return 0;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: zxtxin/QRPay
void main(int argc,char *argv[])
{
	ghttp_request *request = NULL;
	request = ghttp_request_new();
	ghttp_set_uri(request, argv[1]);
	ghttp_set_type(request, ghttp_type_post);
	ghttp_set_body(request, argv[2],strlen(argv[2]));
	printf("%s\n", argv[2]);

	ghttp_prepare(request);
	ghttp_process(request);
	fwrite(ghttp_get_body(request), ghttp_get_body_len(request), 1, stdout);
	fprintf(stdout, "  Length: %s\n", ghttp_get_header(request, "Content-Length"));
	fprintf(stdout, "  Location: %s\n", ghttp_get_header(request, "Location"));
	
}
Exemplo n.º 5
0
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;
}
Exemplo n.º 6
0
int get_http_request(char * url, char * result, int result_len){
	
	int64 post_begin = get_tick_count();
	
	ghttp_request * request = ghttp_request_new();
	if(!request){
		log_out(log_error, "http_get::ghttp_request_new fail\n");
		return -1;
	}

	if(ghttp_set_uri(request, (char*)url) == -1){
		log_out(log_error, "http_get::ghttp_set_uri fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(ghttp_set_type(request, ghttp_type_get) == -1) {
		log_out(log_error, "http_get::ghttp_set_type fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	//����ͷ
	ghttp_set_header(request, http_hdr_Accept_Charset, "x-www-form-urlencoded");
	ghttp_set_header(request, http_hdr_Connection, "close");
	ghttp_set_header(request, http_hdr_Content_Type, "json");
	ghttp_set_header(request, http_hdr_Timeout, "5000");  


	if(ghttp_prepare(request) < 0){
		log_out(log_error, "http_get::ghttp_prepare fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	ghttp_status req_status = ghttp_process(request);
	if (req_status == ghttp_error) {
		log_out(log_error, "http_get::ghttp_process url(%s) fail(%s)\n",
			url, ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	int stat_code = ghttp_status_code(request);
	if(stat_code != 200){
		log_out(log_error, "http_get::status code(%d)\n", stat_code);
		ghttp_request_destroy(request);
		return -1;
	}
	
	int	rsplen = ghttp_get_body_len(request);
	char * rspbody = ghttp_get_body(request);
	if((!rspbody) || (rsplen <= 0)){
		log_out(log_error, "http_get::ghttp_get_body fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}
	
	if(rspbody[rsplen -1] == '\n')rsplen--;
	int ncopy = result_len < rsplen ? result_len : rsplen;
	strncpy(result, rspbody, ncopy);
	ghttp_request_destroy(request);
	
	uint take = (uint)(get_tick_count() - post_begin);
	log_out(log_debug, "http_get::take(%ums) response(%s)\n", take, result);
	
	return 0;
}
Exemplo n.º 7
0
int post_http_request(char * url, char * json, int len, 
				char * result, int result_len){
	int64 post_begin = get_tick_count();
	ghttp_request * request = ghttp_request_new();
	if(!request){
		log_out(log_error, "http_post::ghttp_request_new fail\n");
		return -1;
	}

	if(ghttp_set_uri(request, (char*)url) == -1){
		log_out(log_error, "http_post::ghttp_set_uri fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(ghttp_set_type(request, ghttp_type_post) == -1) {
		log_out(log_error, "http_post::ghttp_set_type fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	//����ͷ
	ghttp_set_header(request, http_hdr_Accept_Charset, "x-www-form-urlencoded");
	ghttp_set_header(request, http_hdr_Connection, "keep-alive");
	ghttp_set_header(request, http_hdr_Content_Type, "json");
	//ghttp_set_header(request, http_hdr_Authorization, "Basic c2hlbmdhbmc6MTIzNDU2");
	ghttp_set_header(request, http_hdr_Timeout, "5000");  
	char content_len[16] = {0};
	sprintf(content_len, "%d", len);
	ghttp_set_header(request, http_hdr_Content_Length, content_len);

	if(ghttp_set_body(request, json, len) == -1){
		log_out(log_error, "http_post::ghttp_set_body fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(ghttp_prepare(request) < 0){
		log_out(log_error, "http_post::ghttp_prepare fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	ghttp_status req_status = ghttp_process(request);
	if (req_status == ghttp_error){
		log_out(log_error, "http_post::ghttp_process url(%s) fail(%s)\n",
				url, ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	int stat_code = ghttp_status_code(request);
	if(stat_code != 200){
		log_out(log_error, "http_post::data len(%d) status code(%d)\n", len, stat_code);
		ghttp_request_destroy(request);
		return -1;
	}

	int	rsplen = ghttp_get_body_len(request);
	char * rspbody = ghttp_get_body(request);
	if((!rspbody) || (rsplen <= 1)){
		log_out(log_error, "http_post::ghttp_get_body fail(%s)\n", ghttp_get_error(request));
		ghttp_request_destroy(request);
		return -1;
	}

	if(rspbody[rsplen - 1] == '\n'){
		rsplen--;}
	int ncopy = (result_len <= rsplen) ? result_len -1 : rsplen;
	if(result){
		strncpy(result, rspbody, ncopy);
	}

	ghttp_request_destroy(request);
	
	uint take = (uint)(get_tick_count() - post_begin);
	log_out(log_debug, "http_post::data len(%d) take(%ums) response(%s)\n", len, take, result);
	
	return 0;				
}
Exemplo n.º 8
0
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;
}
Exemplo n.º 9
0
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;
}