nginx send back content post by client
来源:互联网 发布:智慧树网络课程好过吗? 编辑:程序博客网 时间:2024/06/18 07:04
试了下post 内容到nginx,然后在nginx把该内容原样返回给client,内容在http body填充。
网上找不到可以参考的例子,代码是边查资料边结合源码边用gdb调试写出来的,也不知道API用得对不对,流程对不对,先记下来,再慢慢深入以及完善。
/***/#include <ngx_core.h>#include <ngx_http.h>#include <ngx_config.h>static ngx_str_t data = ngx_string("nginx.\n");static char *ngx_http_test_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static ngx_int_t ngx_http_test_handler(ngx_http_request_t *r);static void ngx_http_test_body_handler(ngx_http_request_t *r);static ngx_command_t ngx_http_test_commands[] = { { ngx_string("test"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_test_init, NGX_HTTP_LOC_CONF_OFFSET, 0, NULL}, ngx_null_command};static ngx_http_module_t ngx_http_test_ctx = { NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};ngx_module_t ngx_http_test_module = { NGX_MODULE_V1,&ngx_http_test_ctx,ngx_http_test_commands,NGX_HTTP_MODULE,NULL, /* init master */ NULL, /* init module */NULL, /* init process */NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */NULL, /* exit master */ NGX_MODULE_V1_PADDING};static char *ngx_http_test_init(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf;clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);clcf->handler = ngx_http_test_handler;/* *ngx_conf_set_str_slot(cf,cmd,conf); */return NGX_CONF_OK;}static ngx_int_tngx_http_test_handler(ngx_http_request_t *r) { ngx_int_t rc;/*if (r->method & NGX_HTTP_POST) { return NGX_HTTP_NOT_ALLOWED;}rc = ngx_http_discard_request_body(r);if (NGX_OK != rc) { return rc;}*/ rc = ngx_http_read_client_request_body(r, ngx_http_test_body_handler); if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { return rc; }return NGX_OK;}static voidngx_http_test_body_handler(ngx_http_request_t *r) { ngx_buf_t *b; ngx_int_t rc; ngx_chain_t out, body;r->headers_out.status = NGX_HTTP_OK;//r->headers_out.content_length_n = data.len;r->headers_out.content_type.data = (u_char *) "text/html";r->headers_out.content_type.len = sizeof("text/html") - 1;if (r->method & NGX_HTTP_HEAD) { rc = ngx_http_send_header(r);/*if (NGX_OK != rc) { return rc;}*/return;}b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));if (NULL == b) { return;// NGX_HTTP_INTERNAL_SERVER_ERROR;}b->pos = data.data;b->last = data.data + data.len;b->last_buf = 1;b->memory = 1;out.buf = b;out.next = NULL;body.buf = r->request_body->bufs->buf;body.next = &out;r->headers_out.content_length_n = body.buf->last - body.buf->pos + data.len;rc = ngx_http_send_header(r);if (NGX_OK != rc) { return;// rc;}/*return */ngx_http_output_filter(r, &body/*&out*/);}
用curl测试:
curl -X POST -H "charset:gb2312" -d "content=因为登陆服务升级,密码策略变更,以前的试脚本中的用户密码已经不能登陆&number=1" http://192.168.0.246:80/test/
返回:(nginx.\r\nhello world!是在原内容末尾顺便加上的。)
content=因为登陆服务升级,密码策略变更,以前的试脚本中的用户密码已经不能登陆&number=1nginx.hello world!
这里有篇文章对request_body的原理作详细说明的文章:
http://chenzhenianqing.cn/articles/764.html
0 0
- nginx send back content post by client
- Tip - 如何解决ASP.NET RadEditor 没有将Content post back
- send a client certificate by using the HttpWebRequest and HttpWebResponse classes
- nginx报错 client intended to send too large body: 1331696 bytes
- nginx报错 client intended to send too large body: 1331696 bytes
- loadrunner11解决Failed to send data by channels - post message failed
- Error: Failed to send data by channels – post message failed
- Failed to send data by channels - post message failed错误解决
- LoadRunner运行场景,错误---Error: Failed to send data by channels - post message failed.
- Send Mail By CDO
- android文件上传413 nginx [error] 9679#0: *318855 client intended to send too large body: 1593409 bytes
- POST 400 Bad Request The request sent by the client was syntactically incorrect
- ajax之get、post、send
- imap send email by python
- auto send password by scp
- go client get/post
- A Client to Send SMTP Mail
- Error :Request header field Content-Type is not allowed by Access-Control-Allow-Headers post请求失败原因
- Python replace()方法
- UNP卷1:第十四章(高级I/O函数)
- LaTeX完整例子_参考文献、图、表和公式
- 求哈弗曼编码
- hdu 1874 dijsktra(mlogn) +bellmanford
- nginx send back content post by client
- 设置字体相关命令
- <图形图像,动画,多媒体> 读书笔记 --- 音效
- [HDU 4776 Ants] Trie异或+预处理
- 寻找阿里巴巴:杨致远完成硅谷史上最赚钱赌博
- 【无限互联】SDWebImage图片缓存流程分析
- c++命令提示符窗口下打印指定大小的菱形代码
- 找到IAR 6.2/6.3编译以前项目的出一大堆错误的原因了
- Intent