-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_fun_module.c
351 lines (298 loc) · 11.5 KB
/
ngx_http_fun_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
//#include <cairo.h>
#include "redis_nginx_adapter/redis_nginx_adapter.h"
#include "hiredis/hiredis.h"
#define M_PI 3.14159265
/*Declare a handler*/
//redisAsyncContext *redis_nginx_open_context(const char *host, int port, int database, redisAsyncContext **context);
//void redis_nginx_force_close_context(redisAsyncContext **context);
//void redis_nginx_ping_callback(redisAsyncContext *ac, void *rep, void *privdata);
void freeReplyObject(void *reply);
redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
void *redisCommand(redisContext *c, const char *format, ...);
void freeReplyObject(void *reply);
//static u_char ngx_fun_string[];// = "This is fun!";
static char *ngx_http_fun(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_fun2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
/* The HTTP BOdY The hello world string. */
//static u_char ngx_fun_string[] = "This is Fun hello world";
/*handler for the request */
static ngx_int_t ngx_http_fun_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_fun_handler2(ngx_http_request_t *r);
static ngx_command_t ngx_http_fun_commands[] = {
{ //entry point fun
ngx_string("fun_http"), /* directive */
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes
no arguments*/
//A pointer to our handler-function
ngx_http_fun, /* configuration setup function */
0, /* No offset. Only one context is supported. */
0, /* No offset when storing the module configuration on struct. */
NULL},
{ //entry point for fun2
ngx_string("fun_http2"), /* directive */
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes
no arguments*/
//A pointer to our handler-function
ngx_http_fun2, /* configuration setup function */
0, /* No offset. Only one context is supported. */
0, /* No offset when storing the module configuration on struct. */
NULL},
ngx_null_command /* command termination */
};
/* The module context. */
static ngx_http_module_t ngx_http_fun_module_ctx = {
NULL, /* 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 */
};
/* Module definition. */
ngx_module_t ngx_http_fun_module = {
NGX_MODULE_V1,
&ngx_http_fun_module_ctx, /* module context */
ngx_http_fun_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
};
//this structure will be used to callback function we're going to to create later.
struct closure{
ngx_http_request_t *r ;
ngx_chain_t * chain;
uint32_t length;
};
/**
* Content handler.
*
* @param r
* Pointer to the request structure. See http_request.h.
* @return
* The status of the response generation.
*/
// static cairo_status_t copy_png_to_chain(void * closure, const unsigned char * data, unsigned int length)
// { // closure is a struct closure
// struct closure * c = closure;
// // Just a helper pointer, to help us traverse the linked list.
// ngx_chain_t * ch = c->chain; // We track the size of the png-file in our closure struct.
// c->length += length;
// ngx_buf_t * b = ngx_pcalloc(c->r->pool, sizeof(ngx_buf_t));
// unsigned char * d = ngx_pcalloc(c->r->pool, length);
// // We make sure to fail if were unable to allocate memory.
// if (b == NULL || d == NULL) {
// return CAIRO_STATUS_NO_MEMORY;}
// // Copy data to our new buffer, and set the pointers.
// ngx_memcpy(d, data, length);
// b->pos = d;
// b->last = d + length;b->memory = 1;
// b->last_buf = 1;
// if ( c->chain->buf == NULL ){
// c->chain->buf = b;
// return CAIRO_STATUS_SUCCESS;
// }
// while ( ch->next ){
// ch = ch->next;
// }
// ch->next = ngx_pcalloc(c->r->pool, sizeof(ngx_chain_t));
// if ( ch->next == NULL ) {
// return CAIRO_STATUS_NO_MEMORY;
// } // Attach our buffer at the end.
// ch->next->buf = b;
// ch->next->next = NULL;
// return CAIRO_STATUS_SUCCESS;
// }
static ngx_int_t ngx_http_fun_handler(ngx_http_request_t *r){
ngx_int_t rc;
//ngx_buf_t * b;
ngx_chain_t out;
struct closure c = {r, &out, 0};
/**FOR FUN make it commmented**/
//We response GET and Head request only
// if(!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))){
// return NGX_HTTP_NOT_ALLOWED;
// }
// //discard request body since we dont need it here
// rc= ngx_http_discard_request_body(r);
// if(rc !=NGX_OK){
// return rc;
// }
//set the content type header
// r->headers_out.content_type_len=sizeof("text/html")-1;
// r->headers_out.content_type.len=sizeof("text/html")-1;
// r->headers_out.content_type.data=(u_char *) "text/html";
//send the header only, if request type is http 'head'
// if(r->method==NGX_HTTP_HEAD){
// r->headers_out.status=NGX_HTTP_OK;
// r->headers_out.content_length_n=sizeof(ngx_fun_string)-1;
// return ngx_http_send_header(r);
// }
//allocate a buffer for your response body
// b =ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
// if(b==NULL){
// return NGX_HTTP_INTERNAL_SERVER_ERROR;
// }
// cairo_surface_t *surface;
// cairo_t *cr;
// surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 256, 256);
// cr= cairo_create(surface);
// double xc=128.0;
// double yc=128.0;
// double radious =100.00;
// double angle1=270.0*(M_PI/180.0);
// double angle2=180.0*(M_PI/180.0);
// cairo_set_line_width(cr,10.0);
// cairo_arc(cr, xc,yc, radious,angle1, angle2);
// cairo_stroke(cr);
// cairo_set_source_rgba(cr,1,0.2,0.2,0.6);
// cairo_set_line_width(cr,6.0);
// cairo_arc(cr,xc,yc,radious,angle1,angle2);
// cairo_line_to(cr,xc,yc);
// cairo_arc(cr,xc,yc,radious,angle1,angle2);
// cairo_line_to(cr,xc,yc);
// cairo_stroke(cr);
// attach this buffer to the buffer chain
out.buf=NULL;
out.next=NULL;
//adjust the pointer of the buffer
// b->pos=ngx_fun_string;
// b->last=ngx_fun_string + sizeof(ngx_fun_string) - 1;
// b->memory=1; //This buffer is read only memory
// // This means that filter should copy it, and not try to rewrite in place
// b->last_buf=1;
// // set the status line
// rc=cairo_surface_write_to_png_stream(surface, copy_png_to_chain, &c);
// cairo_destroy(cr);
// cairo_surface_destroy(surface);
// if(rc != CAIRO_STATUS_SUCCESS)
// {
// return NGX_HTTP_INTERNAL_SERVER_ERROR;
// }
// set the content type header
r->headers_out.content_type_len=sizeof("image/png")-1;
r->headers_out.content_type.len=sizeof("image/png")-1;
r->headers_out.content_type.data=(u_char *) "image/png";
r->headers_out.status= NGX_HTTP_OK;
//r->headers_out.content_length_n=sizeof(ngx_fun_string)-1;
r->headers_out.content_length_n=c.length;
rc=ngx_http_send_header(r);
// send the headers of your response
if(rc==NGX_ERROR || rc > NGX_OK || r->header_only||r->method==NGX_HTTP_HEAD){
return rc;
}
//send the buffer chain of your response
return ngx_http_output_filter(r,&out);
}
//**trying to put something in the text**////
static ngx_int_t ngx_http_fun_handler2(ngx_http_request_t * r){
//unsigned int j;
redisContext *c;
redisReply *reply;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout("127.0.0.1", 6379, timeout);
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\n", c->errstr);
redisFree(c);
} else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
/* PING server */
reply = redisCommand(c,"AUTH %s","redis@123@Azure");
ngx_write_stdout(reply->str);
printf("PING: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key */
reply = redisCommand(c,"SET %s %s", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key using binary safe API */
reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);
// redis_nginx_init();
//redisAsyncContext *redis_server = NULL;
//if (redis_nginx_open_context((const char *) "localhost", 6379, 0, &redis_server) != NULL) {
// redisAsyncCommand(redis_server, NULL, NULL, "SET %b %d-%d", r->uri.data, r->uri.len, r->headers_out.status, r->headers_out.content_length_n);
//}
//redis_nginx_close_context(&redis_server);
u_char ngx_fun_string[]="This is test 102";//(u_char *)r->uri.data;
ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"this is value of request data1 %c---------> data2 %c----->data3 %c body %c----> end",r->uri.data,r->headers_out.status,r->headers_in.host);
ngx_int_t rc;
ngx_buf_t * b;
ngx_chain_t out;
// we response to 'GET' and 'HEAD' requests only
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
return NGX_HTTP_NOT_ALLOWED;
}
// discard request body, since we don't need it here
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
// set the 'Content-type' header
r->headers_out.content_type_len = sizeof("text/html") - 1;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char * ) "text/html";
// send the header only, if the request type is http 'HEAD'
if (r->method == NGX_HTTP_HEAD) {
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = sizeof(ngx_fun_string) - 1;
return ngx_http_send_header(r);
}
// allocate a buffer for your response body
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
// attach this buffer to the buffer chain
out.buf = b;
out.next = NULL;
// adjust the pointers of the buffer
b->pos = ngx_fun_string;
b->last = ngx_fun_string + sizeof(ngx_fun_string) - 1;
b->memory = 1;
// This buffer is in read-only memory
// This means that filters should copy it, and not try to rewrite in place.
b->last_buf = 1; // this is the last buffer in the buffer chain
// set the status line
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = sizeof(ngx_fun_string) - 1;
// send the headers of your response
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
// send the buffer chain of your response
return ngx_http_output_filter(r, &out);
//return ngx_http_send_response(r, NGX_HTTP_OK, &ngx_http_gif_type, &cv);
}
/*Attach the handler
in this handler-function we can validate on the incoming ngx_command_t and attach different handlers*/
static char *ngx_http_fun(ngx_conf_t * cf, ngx_command_t *cmd, void * 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_fun_handler;//handler to process the fun directice
return NGX_CONF_OK;
}
static char *ngx_http_fun2(ngx_conf_t * cf, ngx_command_t *cmd, void * 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_fun_handler2;//handler to process the fun directice
return NGX_CONF_OK;
}
/****Till here every thing workds as earlier****/
/////**********to search the headers*********////////////////