Hi
I'm attempting to write my first plugin, it's going swimmingly except
it's not.
I'm trying to add to the SSI module the same way the perl module does
and have spent a lot of time flicking between the two (c isn't one of my
strong languages).
It appears to work, the very last log line does get called, then it
segfaults
2012/03/16 20:14:55 [emerg] 10663#0: *1 working with the bufferzzzzzz
while sending response to client, client: 118.xx.xx.66, server:
flatus.xx.net, request: "GET /index.html HTTP/1.1", host:
"flatus.xx.net:8081"
2012/03/16 20:14:55 [emerg] 10663#0: *1 working with the buffer failing
btw YqEgHCzpROFbVQPah9mA_w==,1331892955 while sending response to
client, client: 118.xx.xx.66, server: flatus.xx.net, request: "GET
/index.html HTTP/1.1", host: "flatus.xx.net:8081"
I've attached the plugin, such as it is...
You get the log message from line 130
ngx_log_error(NGX_LOG_EMERG, c->log, 0, "working with the buffer failing
btw %V", &foo);
but then it promptly crashes....
Any ideas? I thought it was going along so well :(
Thanks
Shannon.
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
typedef struct {
ngx_http_ssi_ctx_t * ssi;
} ngx_http_secure_link_ssi_ctx_t;
static ngx_int_t ngx_http_secure_link_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params);
static ngx_int_t ngx_http_secure_link_ssi_preconfig(ngx_conf_t *cf);
static ngx_http_module_t ngx_http_secure_link_ssi_ctx = {
ngx_http_secure_link_ssi_preconfig, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_secure_link_ssi_module = {
NGX_MODULE_V1,
&ngx_http_secure_link_ssi_ctx, /* module context */
NULL, /* module directives */
NGX_HTTP_MODULE, /* module type */
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 ngx_http_ssi_param_t ngx_http_secure_link_ssi_params[] = {
{ ngx_string("secret"), 0, 1, 0 },
{ ngx_string("path"), 1, 1, 0 },
{ ngx_string("ttl"), 2, 1, 0 },
{ ngx_null_string, 0, 0, 0 }
};
static ngx_http_ssi_command_t ngx_http_secure_link_ssi_command = {
ngx_string("secure_link"), ngx_http_secure_link_ssi, ngx_http_secure_link_ssi_params, 0, 0, 1
};
static ngx_int_t ngx_http_secure_link_ssi(ngx_http_request_t *r, ngx_http_ssi_ctx_t *ssi_ctx, ngx_str_t **params) {
ngx_str_t smd5, target, strtimeout;
ngx_md5_t md5;
u_char md5_buf[16], *p;
time_t timeout;
size_t len;
ngx_buf_t *b;
ngx_chain_t *cl;
ngx_connection_t *c;
c = r->connection;
timeout = ngx_time() + ngx_atotm(params[2]->data, params[2]->len);
strtimeout.data = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
if (strtimeout.data == NULL) {
return NGX_ERROR;
}
strtimeout.len = ngx_sprintf(strtimeout.data, "%T", timeout) - strtimeout.data;
ngx_md5_init(&md5);
ngx_md5_update(&md5, strtimeout.data, strtimeout.len);
ngx_md5_update(&md5, ".", 1);
ngx_md5_update(&md5, params[1]->data, params[1]->len);
ngx_md5_update(&md5, ".", 1);
ngx_md5_update(&md5, params[0]->data, params[0]->len);
ngx_md5_final(md5_buf, &md5);
smd5.len = 16;
smd5.data = md5_buf;
ngx_encode_base64(&target, &smd5);
size_t count = 0;
for (count = 0; count < target.len; count++) {
if (target.data[count] == '+') {
target.data[count] = '-';
}
else if (target.data[count] == '/') {
target.data[count] = '_';
}
}
p = ngx_pnalloc(r->pool, target.len + 1 + strtimeout.len);
len = ngx_sprintf(p, "%V,%V", &target, &strtimeout) - p;
b = ngx_calloc_buf(r->pool);
if (b == NULL) {
return 1;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return 1;
}
ngx_log_error(NGX_LOG_EMERG, c->log, 0, "working with the buffer");
cl->buf = b;
ngx_str_t foo;
foo.len = len;
foo.data = p;
b->memory = 1;
b->pos = foo.data;
b->last = foo.data + foo.len;
ngx_log_error(NGX_LOG_EMERG, c->log, 0, "working with the bufferzzzzzz");
cl->next = NULL;
*ssi_ctx->last_out = cl;
ssi_ctx->last_out = &cl->next;
ngx_log_error(NGX_LOG_EMERG, c->log, 0, "working with the buffer failing btw %V", &foo);
return NGX_OK;
}
static ngx_int_t ngx_http_secure_link_ssi_preconfig(ngx_conf_t *cf) {
ngx_int_t rc;
ngx_http_ssi_main_conf_t *smcf;
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "it's loaded man...");
smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_ssi_filter_module);
rc = ngx_hash_add_key(&smcf->commands, &ngx_http_secure_link_ssi_command.name,
&ngx_http_secure_link_ssi_command, NGX_HASH_READONLY_KEY);
if (rc != NGX_OK) {
if (rc == NGX_BUSY) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"conflicting SSI command \"%V\"",
&ngx_http_secure_link_ssi_command.name);
}
return NGX_ERROR;
}
return NGX_OK;
}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel