Welcome! Log In Create A New Profile

Advanced

Re: Генерация ответа для клиента и асинхронные сокеты

September 26, 2013 03:06AM
И ещё вопрос: я ничего не упускаю при создании программы?
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_hash.h>
#include <math.h>
#include <string.h>

static void* ngx_http_mymodule_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_mymodule_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);

// Upstream-version
static char* ngx_http_mymodule (ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_mymodule_upstream_handler (ngx_http_request_t *r);

//--------------------------------------------------------------------------
static ngx_command_t ngx_http_mymodule_commands[] = {
{ ngx_string("mymodule"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_mymodule,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },

ngx_null_command
};


//--------------------------------------------------------------------------
static ngx_http_module_t ngx_http_mymodule_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */

NULL, /* create main configuration */
NULL, /* init main configuration */

NULL, /* create server configuration */
NULL, /* merge server configuration */

ngx_http_mymodule_create_loc_conf, /* create location configuration */
ngx_http_mymodule_merge_loc_conf /* merge location configuration */
};

//--------------------------------------------------------------------------
// Описание модуля подсоединения к CoreManager
//----
ngx_module_t ngx_http_mymodule_module = {
NGX_MODULE_V1,
&ngx_http_mymodule_module_ctx, /* module context */
ngx_http_mymodule_commands, /* 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 void *
ngx_http_mymodule_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_mymodule_loc_conf_t *conf;

conf = ngx_pcalloc( cf->pool, sizeof(ngx_http_mymodule_loc_conf_t) );
if( conf == NULL ) {
return NGX_CONF_ERROR;
}

// Конфигурация upstream'а
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;

conf->upstream.pass_request_headers = NGX_CONF_UNSET;
conf->upstream.pass_request_body = NGX_CONF_UNSET;

ngx_str_set(&conf->upstream.module, "mymodule");

return conf;
}

//--------------------------------------------------------------------------
// Функция вызывается при прочтении конфигурации, сливает параметры, записанные
// в конфигурации в нескольких местах, иначе выставляет параметрам значения
// по умолчанию.
//--------------------------------------------------------------------------
static char *
ngx_http_mymodule_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_mymodule_loc_conf_t *prev = parent;
ngx_http_mymodule_loc_conf_t *conf = child;

// Совмещение конфигурации upstream'а
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
prev->upstream.connect_timeout, 60000);

ngx_conf_merge_msec_value(conf->upstream.send_timeout,
prev->upstream.send_timeout, 60000);

ngx_conf_merge_msec_value(conf->upstream.read_timeout,
prev->upstream.read_timeout, 60000);
ngx_conf_merge_value(conf->upstream.pass_request_headers,
prev->upstream.pass_request_headers, 0);
ngx_conf_merge_value(conf->upstream.pass_request_body,
prev->upstream.pass_request_body, 0);

return NGX_CONF_OK;
}


//--------------------------------------------------------------------------
// Функция вызывается когда nginx "натыкается" на команду "mymodule"
// при просмотре конфигурации в секции "location".
//----
static char* ngx_http_mymodule (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_mymodule_loc_conf_t *ispcf = 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_mymodule_upstream_handler;

ngx_str_t *value = cf->args->elts;

ngx_url_t u;
ngx_memzero( &u, sizeof(ngx_url_t) );

u.url = value[1];
u.no_resolve = 1;

ispcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
if( ispcf->upstream.upstream == NULL) {
return NGX_CONF_ERROR;
}

return NGX_CONF_OK;
}

//--------------------------------------------------------------------------
// Функция вызывается, сразу после обращения клиента к серверу
//----
static ngx_int_t ngx_http_mymodule_upstream_handler (ngx_http_request_t *r)
{
if( ngx_http_upstream_create(r) != NGX_OK ) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

ngx_http_mymodule_module_ctx_t * ctx
= ngx_pcalloc( r->pool, sizeof(ngx_http_mymodule_module_ctx_t ) );
if( ctx == NULL ) {
return NGX_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_mymodule_module);

ngx_http_mymodule_loc_conf_t *plcf
= ngx_http_get_module_loc_conf( r, ngx_http_mymodule_module );


if (ngx_http_upstream_create(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}

ngx_http_upstream_t *u = r->upstream;

u->peer.log = r->connection->log;
u->peer.log_error = NGX_ERROR_ERR;
u->output.tag = (ngx_buf_tag_t) &ngx_http_mymodule_module;
u->conf = &plcf->upstream;

u->create_request = ngx_http_mymodule_proxy_create_request;
u->reinit_request = ngx_http_mymodule_proxy_reinit_request;
u->process_header = ngx_http_mymodule_proxy_process_header;
u->abort_request = ngx_http_mymodule_proxy_abort_request;
u->finalize_request = ngx_http_mymodule_proxy_finalize_request;

r->state = 0;
r->upstream = u;
u->buffering = 1;

ngx_int_t rc = ngx_http_read_client_request_body( r, ngx_http_upstream_init );

if( rc >= NGX_HTTP_SPECIAL_RESPONSE ){
return rc;
}

return NGX_DONE;
}

//--------------------------------------------------------------------------
// Генерируем буферы для отправки в сокет
//---
ngx_int_t ngx_http_mymodule_proxy_create_request (ngx_http_request_t *r)
{
ngx_http_upstream_t *up = r->upstream;

// Создаём цепочку буферов запроса
if( up->request_bufs ){
ngx_free_chain( r->pool, up->request_bufs );
}
up->request_bufs = ngx_alloc_chain_link( r->pool );
up->request_bufs->buf = NULL;

// Далее заполняем цепочку буферов //

return NGX_OK;
}
Subject Author Posted

Генерация ответа для клиента и асинхронные сокеты

Aleus Essentia September 23, 2013 02:49AM

Re: Генерация ответа для клиента и асинхронные сокеты

Maxim Dounin September 23, 2013 11:24AM

Re: Генерация ответа для клиента и асинхронные сокеты

Aleus Essentia September 25, 2013 11:36PM

Re: Генерация ответа для клиента и асинхронные сокеты

Maxim Dounin September 26, 2013 08:26AM

Re: Генерация ответа для клиента и асинхронные сокеты

Aleus Essentia October 02, 2013 07:49PM

Re: Генерация ответа для клиента и асинхронные сокеты

Aleus Essentia September 26, 2013 03:06AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 220
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready