<div dir="ltr">hi Maxim,<div><br></div><div style>thanks so much for the code provided, i have merged that code in my module and it worked as expected!. Would you please send me the details to send you the money ?</div><div style>
<br></div><div style>thanks</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 26, 2013 at 7:31 AM, Maxim Dounin <span dir="ltr"><<a href="mailto:mdounin@mdounin.ru" target="_blank">mdounin@mdounin.ru</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello!<br>
<div><div class="h5"><br>
On Fri, Jul 26, 2013 at 12:05:11AM -0600, Julien Zefi wrote:<br>
<br>
> Hi,<br>
><br>
> As i am not able to fix a problem for a NginX module that i am writing, and<br>
> after iterate a couple of times in the mailing list, i am still not able to<br>
> solve the problem. So i would like to offer 100 USD for who is able to help<br>
> me to fix the problem in my test case module.<br>
><br>
> As a reference please check the following thread:<br>
><br>
> <a href="http://mailman.nginx.org/pipermail/nginx-devel/2013-July/003923.html" target="_blank">http://mailman.nginx.org/pipermail/nginx-devel/2013-July/003923.html</a><br>
><br>
> If you think you can fix it, please reply me back in private so we can<br>
> discuss how to proceed,<br>
<br>
</div></div>Below is working example of what you've tried to do. It still<br>
lacks various things like request body handling which should be<br>
here in real module, but it's expected to work.<br>
<br>
Notable problems in your code:<br>
<br>
1) You tried to set low-level event handlers. That's not what<br>
you are supposed to do.<br>
<br>
2) On the other hand, after handling a requests's write event you<br>
are responsible to call ngx_handle_write_event().<br>
<br>
3) The r->count++ operation was done too many times, it's just wrong.<br>
<br>
4) For some unknown reason r->header_only was set. It's just wrong.<br>
<br>
5) Return code from ngx_http_send_header() wasn't handled.<br>
<br>
6) Code returned from the ngx_http_test_handler() was NGX_OK<br>
instead of NGX_DONE which is expected to be used if want to<br>
continue processing.<br>
<br>
7) ... (there were many others, but I'm to lazy to remember all of<br>
them)<br>
<br>
Overral, I would recommend you to read and understand at least<br>
several standard modules _before_ you'll start further coding.<br>
<br>
<br>
#include <ngx_config.h><br>
#include <ngx_core.h><br>
#include <ngx_http.h><br>
<br>
<br>
typedef struct {<br>
ngx_buf_t *buf;<br>
} ngx_http_test_ctx_t;<br>
<br>
<br>
static void ngx_http_test_stream_handler(ngx_http_request_t *r);<br>
static char *ngx_http_test(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);<br>
<br>
<br>
static u_char message[] =<br>
"AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFF"<br>
"AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFF"<br>
"AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFF"<br>
"AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFF"<br>
"AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDDDDDEEEEEEEEEEFFFFFFFFFF"<br>
"\r\n";<br>
<br>
<br>
static ngx_command_t ngx_http_test_case_commands[] = {<br>
{ ngx_string("test_module"),<br>
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,<br>
ngx_http_test,<br>
0,<br>
0,<br>
NULL },<br>
<br>
ngx_null_command<br>
};<br>
<br>
<br>
static ngx_http_module_t ngx_http_test_case_ctx = {<br>
NULL, /* preconfiguration */<br>
NULL, /* postconfiguration */<br>
<br>
NULL, /* create main configuration */<br>
NULL, /* init main configuration */<br>
<br>
NULL, /* create server configuration */<br>
NULL, /* merge server configuration */<br>
<br>
NULL, /* create location configuration */<br>
NULL /* merge location configuration */<br>
};<br>
<br>
<br>
ngx_module_t ngx_http_test_module = {<br>
NGX_MODULE_V1,<br>
&ngx_http_test_case_ctx, /* module context */<br>
ngx_http_test_case_commands, /* module directives */<br>
NGX_HTTP_MODULE, /* module type */<br>
NULL, /* init master */<br>
NULL, /* init module */<br>
NULL, /* init process */<br>
NULL, /* init thread */<br>
NULL, /* exit thread */<br>
NULL, /* exit process */<br>
NULL, /* exit master */<br>
NGX_MODULE_V1_PADDING<br>
};<br>
<br>
<br>
static void<br>
ngx_http_test_stream_handler(ngx_http_request_t *r)<br>
{<br>
ngx_int_t rc;<br>
ngx_buf_t *b;<br>
ngx_chain_t *out, cl;<br>
ngx_http_test_ctx_t *ctx;<br>
<br>
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,<br>
"test stream handler");<br>
<br>
ctx = ngx_http_get_module_ctx(r, ngx_http_test_module);<br>
<br>
if (ctx == NULL) {<br>
/*<br>
* create new context and a buffer we will use to send<br>
* our data<br>
*/<br>
<br>
ctx = ngx_palloc(r->pool, sizeof(ngx_http_test_ctx_t));<br>
if (ctx == NULL) {<br>
ngx_http_finalize_request(r, NGX_ERROR);<br>
return;<br>
}<br>
<br>
ctx->buf = ngx_create_temp_buf(r->pool, sizeof(message));<br>
<br>
ngx_http_set_ctx(r, ctx, ngx_http_test_module);<br>
}<br>
<br>
out = NULL;<br>
b = ctx->buf;<br>
<br>
if (ngx_buf_size(b) == 0) {<br>
/* new buffer or everything send, start over */<br>
<br>
b->pos = b->start;<br>
b->last = ngx_cpymem(b->pos, message, sizeof(message) - 1);<br>
b->flush = 1;<br>
<br>
cl.buf = b;<br>
cl.next = NULL;<br>
<br>
out = &cl;<br>
}<br>
<br>
rc = ngx_http_output_filter(r, out);<br>
<br>
if (rc == NGX_ERROR) {<br>
ngx_http_finalize_request(r, rc);<br>
return;<br>
}<br>
<br>
if (ngx_buf_size(b) == 0) {<br>
ngx_add_timer(r->connection->write, 1);<br>
}<br>
<br>
if (ngx_handle_write_event(r->connection->write, 0) != NGX_OK) {<br>
ngx_http_finalize_request(r, NGX_ERROR);<br>
return;<br>
}<br>
}<br>
<br>
<br>
static ngx_int_t<br>
ngx_http_test_handler(ngx_http_request_t *r)<br>
{<br>
ngx_int_t rc;<br>
<br>
/* set response headers */<br>
<br>
r->headers_out.content_type.len = sizeof("video/mp2t") - 1;<br>
r->headers_out.content_type.data = (u_char *) "video/mp2t";<br>
r->headers_out.status = NGX_HTTP_OK;<br>
<br>
r->write_event_handler = ngx_http_test_stream_handler;<br>
<br>
rc = ngx_http_send_header(r);<br>
<br>
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {<br>
return rc;<br>
}<br>
<br>
r->main->count++;<br>
ngx_http_test_stream_handler(r);<br>
<br>
return NGX_DONE;<br>
}<br>
<br>
<br>
static char *<br>
ngx_http_test(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)<br>
{<br>
ngx_http_core_loc_conf_t *clcf;<br>
<br>
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);<br>
clcf->handler = ngx_http_test_handler;<br>
<br>
return NGX_CONF_OK;<br>
}<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Maxim Dounin<br>
<a href="http://nginx.org/en/donation.html" target="_blank">http://nginx.org/en/donation.html</a><br>
<br>
_______________________________________________<br>
nginx-devel mailing list<br>
<a href="mailto:nginx-devel@nginx.org">nginx-devel@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx-devel" target="_blank">http://mailman.nginx.org/mailman/listinfo/nginx-devel</a><br>
</font></span></blockquote></div><br></div>