|
| 1 | +#include <stdio.h> |
| 2 | +#include "ngx_http.c" |
| 3 | + |
| 4 | +#define HELLO_WORLD "hello world\r\n" |
| 5 | + |
| 6 | +static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
| 7 | +static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r); |
| 8 | + |
| 9 | +/** |
| 10 | + * This module provided directive: hello world. |
| 11 | + * |
| 12 | + */ |
| 13 | +static ngx_command_t ngx_http_hello_world_commands[] = { |
| 14 | + |
| 15 | + { ngx_string("hello_world"), /* directive */ |
| 16 | + NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes |
| 17 | + no arguments*/ |
| 18 | + ngx_http_hello_world, /* configuration setup function */ |
| 19 | + 0, /* No offset. Only one context is supported. */ |
| 20 | + 0, /* No offset when storing the module configuration on struct. */ |
| 21 | + NULL}, |
| 22 | + |
| 23 | + ngx_null_command /* command termination */ |
| 24 | +}; |
| 25 | + |
| 26 | +/* The hello world string. */ |
| 27 | +static u_char ngx_hello_world[] = HELLO_WORLD; |
| 28 | + |
| 29 | +/* The module context. */ |
| 30 | +static ngx_http_module_t ngx_http_hello_world_module_ctx = { |
| 31 | + NULL, /* preconfiguration */ |
| 32 | + NULL, /* postconfiguration */ |
| 33 | + |
| 34 | + NULL, /* create main configuration */ |
| 35 | + NULL, /* init main configuration */ |
| 36 | + |
| 37 | + NULL, /* create server configuration */ |
| 38 | + NULL, /* merge server configuration */ |
| 39 | + |
| 40 | + NULL, /* create location configuration */ |
| 41 | + NULL /* merge location configuration */ |
| 42 | +}; |
| 43 | + |
| 44 | +/* Module definition. */ |
| 45 | +ngx_module_t ngx_http_hello_world_module = { |
| 46 | + NGX_MODULE_V1, |
| 47 | + &ngx_http_hello_world_module_ctx, /* module context */ |
| 48 | + ngx_http_hello_world_commands, /* module directives */ |
| 49 | + NGX_HTTP_MODULE, /* module type */ |
| 50 | + NULL, /* init master */ |
| 51 | + NULL, /* init module */ |
| 52 | + NULL, /* init process */ |
| 53 | + NULL, /* init thread */ |
| 54 | + NULL, /* exit thread */ |
| 55 | + NULL, /* exit process */ |
| 56 | + NULL, /* exit master */ |
| 57 | + NGX_MODULE_V1_PADDING |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Content handler. |
| 62 | + * |
| 63 | + * @param r |
| 64 | + * Pointer to the request structure. See http_request.h. |
| 65 | + * @return |
| 66 | + * The status of the response generation. |
| 67 | + */ |
| 68 | +static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) |
| 69 | +{ |
| 70 | + ngx_buf_t *b; |
| 71 | + ngx_chain_t out; |
| 72 | + |
| 73 | + /* Set the Content-Type header. */ |
| 74 | + r->headers_out.content_type.len = sizeof("text/plain") - 1; |
| 75 | + r->headers_out.content_type.data = (u_char *) "text/plain"; |
| 76 | + |
| 77 | + /* Allocate a new buffer for sending out the reply. */ |
| 78 | + b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); |
| 79 | + |
| 80 | + /* Insertion in the buffer chain. */ |
| 81 | + out.buf = b; |
| 82 | + out.next = NULL; /* just one buffer */ |
| 83 | + |
| 84 | + b->pos = ngx_hello_world; /* first position in memory of the data */ |
| 85 | + b->last = ngx_hello_world + sizeof(ngx_hello_world) - 1; /* last position in memory of the data */ |
| 86 | + b->memory = 1; /* content is in read-only memory */ |
| 87 | + b->last_buf = 1; /* there will be no more buffers in the request */ |
| 88 | + |
| 89 | + /* Sending the headers for the reply. */ |
| 90 | + r->headers_out.status = NGX_HTTP_OK; /* 200 status code */ |
| 91 | + /* Get the content length of the body. */ |
| 92 | + r->headers_out.content_length_n = sizeof(ngx_hello_world) - 1; |
| 93 | + ngx_http_send_header(r); /* Send the headers */ |
| 94 | + |
| 95 | + /* Send the body, and return the status code of the output filter chain. */ |
| 96 | + return ngx_http_output_filter(r, &out); |
| 97 | +} /* ngx_http_hello_world_handler */ |
| 98 | + |
| 99 | +/** |
| 100 | + * Configuration setup function that installs the content handler. |
| 101 | + * |
| 102 | + * @param cf |
| 103 | + * Module configuration structure pointer. |
| 104 | + * @param cmd |
| 105 | + * Module directives structure pointer. |
| 106 | + * @param conf |
| 107 | + * Module configuration structure pointer. |
| 108 | + * @return string |
| 109 | + * Status of the configuration setup. |
| 110 | + */ |
| 111 | +static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| 112 | +{ |
| 113 | + ngx_http_core_loc_conf_t *clcf; /* pointer to core location configuration */ |
| 114 | + |
| 115 | + /* Install the hello world handler. */ |
| 116 | + clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); |
| 117 | + clcf->handler = ngx_http_hello_world_handler; |
| 118 | + |
| 119 | + return NGX_CONF_OK; |
| 120 | +} /* ngx_http_hello_world */ |
| 121 | + |
0 commit comments