HTTP协议处理框架 - 最简单的GET请求拼装和Response解析
来源:互联网 发布:java游戏编程书籍推荐 编辑:程序博客网 时间:2024/06/02 05:06
框架头文件:
/************************ * HTTP Parser framework * 2010-01-11 ************************/#ifndef __SIMPLE_HTTP_H__#define __SIMPLE_HTTP_H__#define GET_REQUEST_SIZE 1024#define HEADER_SIZE 1024#define CONTENT_SIZE 1024000// response headertypedef struct { char content_type[32]; char content_length[32];} http_response;// build a GET requestint httpGET(char * sndbuf, int buflen, const char * http_version, const char * http_useragent, const char * http_host, const char * http_uri);// read a response from a socket// (no reconnection, so your socket must be healthy)int httpResponse(int fd, char * content, int len, http_response * response);#endif
GET请求拼装
const char * HTTP_GET_FORMAT ="GET %s %s/r/n" /"User-Agent: %s/r/n" /"Host: %s/r/n" /"/r/n";
nt httpGET(char * sndbuf, int buflen, const char * http_version, const char * http_useragent, const char * http_host, const char * http_uri){ if (sndbuf == NULL || buflen <= 0) { return -1; } return snprintf(sndbuf, buflen, HTTP_GET_FORMAT, http_uri, http_version, http_useragent, http_host);}
Response解析 -- header 与 Content 切分部分int httpResponse(int fd, char * content, int len, http_response * response){char header[HEADER_SIZE] = { 0 };char c;char line[1024];int cindex = 0;// analyze statusint status = 0;char * contentptr = content;char * headerptr = header;char * content_head = NULL;int readnum = 0;int content_length = 0;int content_rest_length = 0;if (fd < 0 || content == NULL || len <= 0){return -1;}if ((readnum = read(fd, header, HEADER_SIZE)) > 0){while ((c = *headerptr) && content_head == NULL){switch (status){case 0: // nothing happendif (c == '/r') {status = 1;httpParseLine(line, cindex, response); // a whole linecindex = 0;memset(line, 0, sizeof(line));}else {line[cindex++] = c;}headerptr ++;break;case 1: // a /r appearedif (c == '/n') {status = 2;headerptr ++;}else {status = 0;}break;case 2: // a /n followed a /rif (c == '/r') {status = 3;headerptr ++;}else{status = 0;}break;case 3: // a /r followed a /r/n pairif (c == '/n') {status = 4;headerptr ++;}else{status = 0;}break;case 4: // head tailcontent_head = headerptr;content_length = atoi(response->content_length);content_rest_length = content_length - (readnum - (headerptr - header));contentptr = content;strncpy(content, content_head, readnum - (headerptr - header));contentptr += readnum - (headerptr - header);while (content_rest_length > 0){readnum = read(fd, contentptr, content_rest_length);contentptr += readnum;content_rest_length -= readnum;}break;}}}return 0;}
header Parser// parser item in headstatic int httpParseLine(const char * line, int len, http_response * response){ char c; int index = 0; int status = 0; if (line == NULL || len == 0 || response == NULL) { return -1; } while (status != 1) { switch (status) { case 0: // nothing happened if (strncmp(line, "Content-Type:", 13) == 0) { status = 2; } else if (strncmp(line, "Content-Length:", 15) == 0) { status = 3; } else { return -1; } break; case 1: // finished break; case 2: strcpy(response->content_type, line + 13); status = 1; break; case 3: strcpy(response->content_length, line + 15); status = 1; break; } } return 0;}
- HTTP协议处理框架 - 最简单的GET请求拼装和Response解析
- okhttp框架的最简单的基本使用 post和get请求以及用gson解析请求的数据
- HTTP的post和get请求解析
- HTTP协议的GET请求和POST请求
- http协议中,get请求和post请求的区别
- Http协议的请求和响应解析
- Http协议(get请求和post请求)
- HTTP协议的常见的请求方式:GET和POST
- HTTP 协议请求方法get和post的区别
- 基于HTTP 协议的GET和POST请求服务
- http协议请求,get和post的区别与联系
- http协议之get 和 post请求
- HTTP协议GET和POST请求对比
- 【JAVAWEB】--HTTP协议GET和POST请求
- Http协议请求的解析
- Volley框架实现Http的get和post请求
- Java 里http协议的get请求
- 简单封装Http的Get和Post请求
- Android 获取屏幕高度、标题高度、状态栏高度详解
- Android 内存泄漏调试
- windows下的dos 命令
- X680 12 ASN.1词项
- canvas相关(渐变、阴影、path)
- HTTP协议处理框架 - 最简单的GET请求拼装和Response解析
- 文件I/O
- SQL Server2000 未公开的存储过程
- C语言之详解#ifdef等宏(转载)
- Android自动测试之Monkey工具
- C#编写简单的聊天程序
- Tslib算法分析一
- 无人值守安装SQL Server 2005
- DataGrid-数据格式设置表达式