⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ghttp.c

📁 完整实现http协议源代码(WINDOWS或LINUX平台均可移植使用),我在VC++上(不调用WINDOWS的HTTP的API)可实现XML文件下载等.
💻 C
📖 第 1 页 / 共 2 页
字号:
	  a_request->connected = 1;	}      l_rv = http_req_send(a_request->req, a_request->conn);      if (l_rv == HTTP_TRANS_ERR)	return ghttp_error;      if (l_rv == HTTP_TRANS_NOT_DONE)	return ghttp_not_done;      if (l_rv == HTTP_TRANS_DONE)	{	  a_request->proc = ghttp_proc_response_hdrs;	  if (a_request->conn->sync == HTTP_TRANS_ASYNC)	    return ghttp_not_done;	}    }  if (a_request->proc == ghttp_proc_response_hdrs)    {      l_rv = http_resp_read_headers(a_request->resp, a_request->conn);      if (l_rv == HTTP_TRANS_ERR)	return ghttp_error;      if (l_rv == HTTP_TRANS_NOT_DONE)	return ghttp_not_done;      if (l_rv == HTTP_TRANS_DONE)	{	  a_request->proc = ghttp_proc_response;	  if (a_request->conn->sync == HTTP_TRANS_ASYNC)	    return ghttp_not_done;	}    }  if (a_request->proc == ghttp_proc_response)    {      l_rv = http_resp_read_body(a_request->resp,				 a_request->req,				 a_request->conn);      if (l_rv == HTTP_TRANS_ERR)	{	  /* make sure that the connected flag is fixed and stuff */	  if (a_request->conn->sock == -1)	    a_request->connected = 0;	  return ghttp_error;	}      if (l_rv == HTTP_TRANS_NOT_DONE)	return ghttp_not_done;      if (l_rv == HTTP_TRANS_DONE)	{	/* make sure that the connected flag is fixed and stuff */	  if (a_request->conn->sock == -1)	    a_request->connected = 0;	  a_request->proc = ghttp_proc_none;	  return ghttp_done;	}    }  return ghttp_error;}ghttp_current_statusghttp_get_status(ghttp_request *a_request){  ghttp_current_status l_return;  l_return.proc = a_request->proc;  if (a_request->proc == ghttp_proc_request)    {      l_return.bytes_read = a_request->conn->io_buf_io_done;      l_return.bytes_total = a_request->conn->io_buf_alloc;    }  else if (a_request->proc == ghttp_proc_response_hdrs)    {      l_return.bytes_read = 0;      l_return.bytes_total = 0;    }  else if (a_request->proc == ghttp_proc_response)    {      if (a_request->resp->content_length > 0)	{          l_return.bytes_read = a_request->resp->body_len +            a_request->conn->io_buf_alloc +            a_request->resp->flushed_length;	  l_return.bytes_total = a_request->resp->content_length;	}      else	{	  l_return.bytes_read = a_request->resp->body_len +	    a_request->conn->io_buf_alloc +            a_request->resp->flushed_length;	  l_return.bytes_total = -1;	}    }  else    {      l_return.bytes_read = 0;      l_return.bytes_total = 0;    }  return l_return;}voidghttp_flush_response_buffer(ghttp_request *a_request){  http_resp_flush(a_request->resp, a_request->conn);}intghttp_close(ghttp_request *a_request){  if (!a_request)    return -1;  if (a_request->conn->sock >= 0)    {      close(a_request->conn->sock);      a_request->conn->sock = -1;    }  a_request->connected = 0;  return 0;}voidghttp_clean(ghttp_request *a_request){  http_resp_destroy(a_request->resp);  a_request->resp = http_resp_new();  http_req_destroy(a_request->req);  a_request->req = http_req_new();  http_trans_buf_reset(a_request->conn);  a_request->proc = ghttp_proc_none;  return;}voidghttp_set_chunksize(ghttp_request *a_request, int a_size){  if (a_request && (a_size > 0))    a_request->conn->io_buf_chunksize = a_size;}voidghttp_set_header(ghttp_request *a_request,		 const char *a_hdr, const char *a_val){  http_hdr_set_value(a_request->req->headers,		     a_hdr, a_val);}const char *ghttp_get_header(ghttp_request *a_request,		 const char *a_hdr){  return http_hdr_get_value(a_request->resp->headers,			    a_hdr);}intghttp_get_header_names(ghttp_request *a_request,		       char ***a_hdrs, int *a_num_hdrs){  return http_hdr_get_headers(a_request->resp->headers,			      a_hdrs, a_num_hdrs);}const char *ghttp_get_error(ghttp_request *a_request){  if (a_request->errstr == NULL)    return "Unknown Error.";  return a_request->errstr;}time_tghttp_parse_date(char *a_date){  if (!a_date)    return 0;  return (http_date_to_time(a_date));}intghttp_status_code(ghttp_request *a_request){  if (!a_request)    return 0;  return(a_request->resp->status_code);}const char *ghttp_reason_phrase(ghttp_request *a_request){  if (!a_request)    return 0;  return(a_request->resp->reason_phrase);}intghttp_get_socket(ghttp_request *a_request){  if (!a_request)    return -1;  return(a_request->conn->sock);}char *ghttp_get_body(ghttp_request *a_request){  if (!a_request)    return NULL;  if (a_request->proc == ghttp_proc_none)    return (a_request->resp->body);  if (a_request->proc == ghttp_proc_response)    {      if (a_request->resp->content_length > 0)	{	  if (a_request->resp->body_len)	    return a_request->resp->body;	  else	    return a_request->conn->io_buf;	}      else	{	  return a_request->resp->body;	}    }  return NULL;}intghttp_get_body_len(ghttp_request *a_request){  if (!a_request)    return 0;  if (a_request->proc == ghttp_proc_none)    return (a_request->resp->body_len);  if (a_request->proc == ghttp_proc_response)    {      if (a_request->resp->content_length > 0)	{	  if (a_request->resp->body_len)	    return a_request->resp->body_len;	  else	    return a_request->conn->io_buf_alloc;	}      else	{	  return a_request->resp->body_len;	}    }  return 0;}intghttp_set_authinfo(ghttp_request *a_request,		   const char *a_user,		   const char *a_pass){  char *l_authtoken = NULL;  char *l_final_auth = NULL;  char *l_auth64 = NULL;  /* check our args */  if (!a_request)    return -1;  /* if we have a NULL or zero length string in the username     or password field, blitz the authinfo */  if ((!a_user) || (strlen(a_user) < 1) ||      (!a_pass) || (strlen(a_pass)< 1))    {      if (a_request->username)	{	  free(a_request->username);	  a_request->username = NULL;	}      if (a_request->password)	{	  free(a_request->password);	  a_request->password = NULL;	}      if (a_request->authtoken)	{	  free(a_request->authtoken);	  a_request->authtoken = NULL;	}      return 0;    }  /* encode the string using base64.  Usernames and passwords     for basic authentication are encoded like this:     username:password     That's it.  Easy, huh?  */  /* enough for the trailing \0 and the : */  l_authtoken = malloc(strlen(a_user) + strlen(a_pass) + 2);  memset(l_authtoken, 0, (strlen(a_user) + strlen(a_pass) + 2));  sprintf(l_authtoken, "%s:%s", a_user, a_pass);  l_auth64 = http_base64_encode(l_authtoken);  if (!l_auth64)    {      free(l_authtoken);      return -1;    }  /* build the final header */  l_final_auth = malloc(strlen(l_auth64) + strlen(basic_header) + 1);  memset(l_final_auth, 0, (strlen(l_auth64) + strlen(basic_header) + 1));  strcat(l_final_auth, basic_header);  strcat(l_final_auth, l_auth64);  free(l_auth64);  free(l_authtoken);  /* copy the strings into the request */  if (a_request->username) free(a_request->username);  if (a_request->password) free(a_request->password);  if (a_request->authtoken) free(a_request->authtoken);  a_request->username = strdup(a_user);  a_request->password = strdup(a_pass);  a_request->authtoken = l_final_auth;  return 0;}intghttp_set_proxy_authinfo(ghttp_request *a_request,			 const char *a_user,			 const char *a_pass){  char *l_authtoken = NULL;  char *l_final_auth = NULL;  char *l_auth64 = NULL;    /* check our args */  if (!a_request)    return -1;  /* if we have a NULL or zero length string in the username     or password field, blitz the authinfo */  if ((!a_user) || (strlen(a_user) < 1) ||      (!a_pass) || (strlen(a_pass)< 1))    {      if (a_request->proxy_username)	{	  free(a_request->proxy_username);	  a_request->proxy_username = NULL;	}      if (a_request->proxy_password)	{	  free(a_request->proxy_password);	  a_request->proxy_password = NULL;	}      if (a_request->proxy_authtoken)	{	  free(a_request->proxy_authtoken);	  a_request->proxy_authtoken = NULL;	}      return 0;    }  /* encode the string using base64.  Usernames and passwords     for basic authentication are encoded like this:     username:password     That's it.  Easy, huh?  */  /* enough for the trailing \0 and the : */  l_authtoken = malloc(strlen(a_user) + strlen(a_pass) + 2);  memset(l_authtoken, 0, (strlen(a_user) + strlen(a_pass) + 2));  sprintf(l_authtoken, "%s:%s", a_user, a_pass);  l_auth64 = http_base64_encode(l_authtoken);  if (!l_auth64)    {      free(l_authtoken);      return -1;    }  /* build the final header */  l_final_auth = malloc(strlen(l_auth64) + strlen(basic_header) + 1);  memset(l_final_auth, 0, (strlen(l_auth64) + strlen(basic_header) + 1));  strcat(l_final_auth, basic_header);  strcat(l_final_auth, l_auth64);  free(l_auth64);  free(l_authtoken);  /* copy the strings into the request */  if (a_request->proxy_username) free(a_request->proxy_username);  if (a_request->proxy_password) free(a_request->proxy_password);  if (a_request->proxy_authtoken) free(a_request->proxy_authtoken);  a_request->proxy_username = strdup(a_user);  a_request->proxy_password = strdup(a_pass);  a_request->proxy_authtoken = l_final_auth;    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -