HTTP学习资料 1. 需求背景 WebClient主要用来传输文件 WebNet用来支持cgi接口,需要支持get post put delete方式
2. webnet中使用 2.1 webnet存在问题 2.11 rt-thread 使用中option访问没有得到期望的响应,为了不改变底层组件库,自己在外部加了专门单独针对OPTION的响应
http 响应状态在rt-thread中只有200才是成功, 其它会报错误,要改下
void webnet_session_set_header_option(struct webnet_session *session, int code, const char* title)
{
int length = 0;
static const char* fmt = "HTTP/1.1 %d %s\r\n%s";
static const char* Access = "Access-Control-Allow-Origin: *\r\n";
static const char* Methods = "Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS\r\n";
static const char* Headers = "Access-Control-Allow-Headers: Content-Type\r\n";
static const char* Age = "Access-Control-Max-Age: 86400\r\n";
static const char* content = "Content-Type: %s\r\nContent-Length: %ld\r\nConnection: %s\r\n\r\n";
static const char* content_nolength = "Content-Type: %s\r\nConnection: %s\r\n\r\n";
char *ptr, *end_buffer;
int offset;
ptr = (char*)session->buffer;
end_buffer = (char*)session->buffer + session->buffer_length;
offset = rt_snprintf(ptr, end_buffer - ptr, fmt, code, title, WEBNET_SERVER);
ptr += offset;
offset = rt_snprintf(ptr, end_buffer - ptr, Access);
ptr += offset;
offset = rt_snprintf(ptr, end_buffer - ptr, Methods);
ptr += offset;
offset = rt_snprintf(ptr, end_buffer - ptr, Headers);
ptr += offset;
offset = rt_snprintf(ptr, end_buffer - ptr, Age);
ptr += offset;
offset = rt_snprintf(ptr, end_buffer - ptr, "Connection: Keep-Alive\r\n\r\n");
ptr += offset;
length = ptr - (char*)session->buffer;
if (webnet_module_handle_event(session, WEBNET_EVENT_RSP_HEADER) == WEBNET_MODULE_CONTINUE)
{
webnet_session_write(session, session->buffer, length);
}
}
2.12 对于PUT DELTEE处理错误, 这里取巧了,懒的研究代码
request->origin_method = request->method;
if( (WEBNET_DELETE == request->method) || (WEBNET_PUT == request->method) ){
request->method = WEBNET_POST;
}
在wn_module_cgi.c文件中修改
3. webclient 使用 webclient 客服端用于文件传输,采用http multipart/form-data单提交方式,由于http基于tcp,tcp基于窗口确认机制其速度远远高于TFTP的停止等待机制,特别是对于远程传输文件。
3.1 GET获取文件可以直接用不用修改 3.2 POST提交文件,由于需要附件信息,因此需要修改
postman中配置 对应的http协议标准格式
POST / HTTP/1.1
Host: e944r8404.oicp.vip
Content-Length: 305
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW #一定要有\r\n换行
----WebKitFormBoundary7MA4YWxkTrZu0gW # boundary 分割符
Content-Disposition: form-data; name=""; filename="/C:/Users/admin/Desktop/222222222222222/tmp/t1.txt"
Content-Type: text/plain
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW # boundary 分割符
Content-Disposition: form-data; name="path" # 表单描述
# \r\n
/resource # 内容
----WebKitFormBoundary7MA4YWxkTrZu0gW-- # 最后一个boundary 有--
wireshark抓包, 本次传输两个text表单,两文件表单
4. 关于http 协议
4.1 断点续传参考资料 4.2 http 常规头数据帧格式 whireshak抓包 所有头信息以\r\n分割,头与消息实体也是\r\n分割
|