免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1825 | 回复: 2
打印 上一主题 下一主题

[C] nginx 初试 [复制链接]

论坛徽章:
1
程序设计版块每日发帖之星
日期:2015-08-12 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-08-05 13:19 |只看该作者 |倒序浏览
本帖最后由 framily 于 2015-08-05 13:20 编辑
复制代码
  1. #include <ngx_config.h>
  2. #include <ngx_core.h>
  3. #include <ngx_http.h>

  4. /* Module config */
  5. typedef struct {
  6.         ngx_str_t ed;
  7. } ngx_http_echo_loc_conf_t;

  8. static char *ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  9. static void *ngx_http_echo_create_loc_conf(ngx_conf_t *cf);
  10. static char *ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);

  11. /* Directives */
  12. static ngx_command_t ngx_http_echo_commands[] =
  13. {
  14.         {
  15.                 ngx_string("echo"),
  16.                 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  17.                 ngx_http_echo,
  18.                 NGX_HTTP_LOC_CONF_OFFSET,
  19.                 offsetof(ngx_http_echo_loc_conf_t, ed),
  20.                 NULL
  21.         },
  22.         ngx_null_command
  23. };

  24. /* Http context of the module */
  25. static ngx_http_module_t ngx_http_echo_module_ctx = {
  26.         NULL, /* preconfiguration */
  27.         NULL, /* postconfiguration */
  28.         NULL, /* create main configuration */
  29.         NULL, /* init main configuration */
  30.         NULL, /* create server configuration */
  31.         NULL, /* merge server configuration */
  32.         ngx_http_echo_create_loc_conf, /* create location configration */
  33.         ngx_http_echo_merge_loc_conf /* merge location configration */
  34. };

  35. /* Module */
  36. ngx_module_t ngx_http_echo_module =
  37. {
  38.         NGX_MODULE_V1,
  39.         &ngx_http_echo_module_ctx, /* module context */
  40.         ngx_http_echo_commands, /* module directives */
  41.         NGX_HTTP_MODULE, /* module type */
  42.         NULL, /* init master */
  43.         NULL, /* init module */
  44.         NULL, /* init process */
  45.         NULL, /* init thread */
  46.         NULL, /* exit thread */
  47.         NULL, /* exit process */
  48.         NULL, /* exit master */
  49.         NGX_MODULE_V1_PADDING
  50. };

  51. /* Handler function */
  52. static ngx_int_t
  53. ngx_http_echo_handler(ngx_http_request_t *r)
  54. {
  55.         ngx_int_t rc;
  56.         ngx_buf_t *b;
  57.         ngx_chain_t out;
  58.         ngx_http_echo_loc_conf_t *elcf;
  59.        
  60.         elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
  61.        
  62.         if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
  63.         {
  64.                 return NGX_HTTP_NOT_ALLOWED;
  65.         }
  66.        
  67.         r->headers_out.content_type.len = sizeof("text/html") - 1;
  68.         r->headers_out.content_type.data = (u_char *) "text/html";
  69.         r->headers_out.status = NGX_HTTP_OK;
  70.         r->headers_out.content_length_n = elcf->ed.len;
  71.        
  72.         if(r->method == NGX_HTTP_HEAD)
  73.         {
  74.                 rc = ngx_http_send_header(r);
  75.                
  76.                 if(rc != NGX_OK)
  77.                 {
  78.                         return rc;
  79.                 }
  80.         }
  81.        
  82.         b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
  83.        
  84.         if(b == NULL)
  85.         {
  86.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  87.                                 "Failed to allocate response buffer.");
  88.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  89.         }
  90.        
  91.         out.buf = b;
  92.         out.next = NULL;
  93.         b->pos = elcf->ed.data;
  94.         b->last = elcf->ed.data + (elcf->ed.len);
  95.         b->memory = 1;
  96.         b->last_buf = 1;
  97.         rc = ngx_http_send_header(r);
  98.        
  99.         if(rc != NGX_OK)
  100.         {
  101.                 return rc;
  102.         }
  103.        
  104.         return ngx_http_output_filter(r, &out);
  105. }

  106. static char *
  107. ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  108. {
  109.         ngx_http_core_loc_conf_t *clcf;
  110.         clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  111.         clcf->handler = ngx_http_echo_handler;
  112.         ngx_conf_set_str_slot(cf,cmd,conf);
  113.         return NGX_CONF_OK;
  114. }

  115. static void *
  116. ngx_http_echo_create_loc_conf(ngx_conf_t *cf)
  117. {
  118.         ngx_http_echo_loc_conf_t *conf;
  119.         conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_echo_loc_conf_t));
  120.        
  121.         if (conf == NULL)
  122.         {
  123.                 return NGX_CONF_ERROR;
  124.         }
  125.        
  126.         conf->ed.len = 0;
  127.         conf->ed.data = NULL;
  128.         return conf;
  129. }

  130. static char *
  131. ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
  132. {
  133.         ngx_http_echo_loc_conf_t *prev = parent;
  134.         ngx_http_echo_loc_conf_t *conf = child;
  135.         ngx_conf_merge_str_value(conf->ed, prev->ed, "");
  136.         return NGX_CONF_OK;
  137. }
复制代码

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
2 [报告]
发表于 2015-08-05 18:02 |只看该作者
很好 继续努力,  ngnix 是个好东西, 模块开发适合大公司用。

论坛徽章:
0
3 [报告]
发表于 2015-08-06 11:59 |只看该作者
很好,很牛X。。膜拜。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP