comparison src/mysql/ngx_http_mysql_test.c @ 653:7cbef16c71a1 release-0.3.48

nginx-0.3.48-RELEASE import *) Change: now the ngx_http_charset_module works for subrequests, if the response has no "Content-Type" header line. *) Bugfix: if the "proxy_pass" directive has no URI part, then the "proxy_redirect default" directive add the unnecessary slash in start of the rewritten redirect. *) Bugfix: the internal redirect always transform client's HTTP method to GET, now the transformation is made for the "X-Accel-Redirect" redirects only and if the method is not HEAD; the bug had appeared in 0.3.42. *) Bugfix: the ngx_http_perl_module could not be built, if the perl was built with the threads support; the bug had appeared in 0.3.46.
author Igor Sysoev <igor@sysoev.ru>
date Mon, 29 May 2006 17:28:12 +0000
parents
children 63a820b0bc6c
comparison
equal deleted inserted replaced
652:d01fc553611d 653:7cbef16c71a1
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6 #include <ngx_config.h>
7 #include <ngx_core.h>
8 #include <ngx_mysql.h>
9 #include <ngx_http.h>
10
11
12 typedef struct {
13 ngx_peers_t *peers;
14 } ngx_http_mysql_test_conf_t;
15
16
17 static void ngx_http_mysql_auth(ngx_mysql_t *m);
18 static void ngx_http_mysql_done(ngx_mysql_t *m);
19 static void *ngx_http_mysql_test_create_loc_conf(ngx_conf_t *cf);
20 static char *ngx_http_mysql_test(ngx_conf_t *cf, ngx_command_t *cmd,
21 void *conf);
22
23 static ngx_command_t ngx_http_mysql_test_commands[] = {
24
25 { ngx_string("mysql_test"),
26 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
27 ngx_http_mysql_test,
28 NGX_HTTP_LOC_CONF_OFFSET,
29 0,
30 NULL },
31
32 ngx_null_command
33 };
34
35
36 ngx_http_module_t ngx_http_mysql_test_module_ctx = {
37 NULL, /* preconfiguration */
38 NULL, /* postconfiguration */
39
40 NULL, /* create main configuration */
41 NULL, /* init main configuration */
42
43 NULL, /* create server configuration */
44 NULL, /* merge server configuration */
45
46 ngx_http_mysql_test_create_loc_conf, /* create location configuration */
47 NULL /* merge location configuration */
48 };
49
50
51 ngx_module_t ngx_http_mysql_test_module = {
52 NGX_MODULE_V1,
53 &ngx_http_mysql_test_module_ctx, /* module context */
54 ngx_http_mysql_test_commands, /* module directives */
55 NGX_HTTP_MODULE, /* module type */
56 NULL, /* init master */
57 NULL, /* init module */
58 NULL, /* init process */
59 NULL, /* init thread */
60 NULL, /* exit thread */
61 NULL, /* exit process */
62 NULL, /* exit master */
63 NGX_MODULE_V1_PADDING
64 };
65
66
67 static ngx_str_t ngx_mysql_login = ngx_string("root");
68 static ngx_str_t ngx_mysql_passwd = ngx_string("tp");
69 static ngx_str_t ngx_mysql_database = ngx_string("mysql");
70 static ngx_str_t ngx_mysql_command_query =
71 ngx_string("select * from user");
72
73
74 static ngx_int_t
75 ngx_http_mysql_test_handler(ngx_http_request_t *r)
76 {
77 ngx_int_t rc;
78 ngx_mysql_t *m;
79 ngx_http_mysql_test_conf_t *mtcf;
80
81 mtcf = ngx_http_get_module_loc_conf(r, ngx_http_mysql_test_module);
82
83 m = ngx_pcalloc(r->pool, sizeof(ngx_mysql_t));
84
85 if (m == NULL) {
86 return NGX_HTTP_INTERNAL_SERVER_ERROR;
87 }
88
89 m->pool = r->pool;
90 m->handler = ngx_http_mysql_auth;
91 m->data = r;
92
93 m->login = &ngx_mysql_login;
94 m->passwd = &ngx_mysql_passwd;
95 m->database = &ngx_mysql_database;
96
97 m->peer.log = r->connection->log;
98 m->peer.log_error = NGX_ERROR_ERR;
99 m->peer.peers = mtcf->peers;
100 m->peer.tries = mtcf->peers->number;
101
102 rc = ngx_mysql_connect(m);
103
104 if (rc == NGX_OK || rc == NGX_AGAIN) {
105 return NGX_DONE;
106 }
107
108 return NGX_HTTP_INTERNAL_SERVER_ERROR;
109 }
110
111
112 static void
113 ngx_http_mysql_auth(ngx_mysql_t *m)
114 {
115 ngx_http_request_t *r;
116
117 r = m->data;
118
119 if (m->state != NGX_OK) {
120 ngx_http_finalize_request(r, NGX_HTTP_NO_CONTENT);
121 return;
122 }
123
124 m->query.len = NGX_MYSQL_CMDPKT_LEN + ngx_mysql_command_query.len;
125
126 m->query.data = ngx_palloc(r->pool, m->query.len);
127 if (m->query.data == NULL) {
128 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
129 return;
130 }
131
132 ngx_memcpy(m->query.data + NGX_MYSQL_CMDPKT_LEN,
133 ngx_mysql_command_query.data, ngx_mysql_command_query.len);
134
135 m->handler = ngx_http_mysql_done;
136
137 ngx_mysql_query(m);
138 }
139
140
141 static void
142 ngx_http_mysql_done(ngx_mysql_t *m)
143 {
144 ngx_http_request_t *r;
145
146 r = m->data;
147
148 ngx_http_finalize_request(r, NGX_HTTP_NO_CONTENT);
149 }
150
151
152 static void *
153 ngx_http_mysql_test_create_loc_conf(ngx_conf_t *cf)
154 {
155 ngx_http_mysql_test_conf_t *conf;
156
157 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mysql_test_conf_t));
158 if (conf == NULL) {
159 return NGX_CONF_ERROR;
160 }
161
162 return conf;
163 }
164
165 static char *
166 ngx_http_mysql_test(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
167 {
168 ngx_http_mysql_test_conf_t *mtcf = conf;
169
170 ngx_str_t *value;
171 ngx_url_t u;
172 ngx_http_core_loc_conf_t *clcf;
173
174 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
175 clcf->handler = ngx_http_mysql_test_handler;
176
177 value = cf->args->elts;
178
179 ngx_memzero(&u, sizeof(ngx_url_t));
180
181 u.url = value[1];
182 u.default_portn = 3306;
183
184 if (ngx_parse_url(cf, &u) != NGX_OK) {
185 if (u.err) {
186 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
187 "%s in upstream \"%V\"", u.err, &u.url);
188 }
189
190 return NGX_CONF_ERROR;
191 }
192
193 mtcf->peers = u.peers;
194
195 return NGX_CONF_OK;
196 }