-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ngx_http_lua_input_filters.c
137 lines (97 loc) · 2.45 KB
/
ngx_http_lua_input_filters.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
/*
* Copyright (C) by OpenResty Inc.
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_common.h"
ngx_int_t
ngx_http_lua_read_bytes(ngx_buf_t *src, ngx_chain_t *buf_in, size_t *rest,
ssize_t bytes, ngx_log_t *log)
{
if (bytes == 0) {
return NGX_ERROR;
}
if ((size_t) bytes >= *rest) {
buf_in->buf->last += *rest;
src->pos += *rest;
*rest = 0;
return NGX_OK;
}
/* bytes < *rest */
buf_in->buf->last += bytes;
src->pos += bytes;
*rest -= bytes;
return NGX_AGAIN;
}
ngx_int_t
ngx_http_lua_read_all(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes,
ngx_log_t *log)
{
if (bytes == 0) {
return NGX_OK;
}
buf_in->buf->last += bytes;
src->pos += bytes;
return NGX_AGAIN;
}
ngx_int_t
ngx_http_lua_read_any(ngx_buf_t *src, ngx_chain_t *buf_in, size_t *max,
ssize_t bytes, ngx_log_t *log)
{
if (bytes == 0) {
return NGX_ERROR;
}
if (bytes >= (ssize_t) *max) {
bytes = (ssize_t) *max;
}
buf_in->buf->last += bytes;
src->pos += bytes;
return NGX_OK;
}
ngx_int_t
ngx_http_lua_read_line(ngx_buf_t *src, ngx_chain_t *buf_in, ssize_t bytes,
ngx_log_t *log)
{
u_char *dst;
u_char c;
#if (NGX_DEBUG)
u_char *begin;
#endif
#if (NGX_DEBUG)
begin = src->pos;
#endif
if (bytes == 0) {
return NGX_ERROR;
}
dd("already read: %p: %.*s", buf_in,
(int) (buf_in->buf->last - buf_in->buf->pos), buf_in->buf->pos);
dd("data read: %.*s", (int) bytes, src->pos);
dst = buf_in->buf->last;
while (bytes--) {
c = *src->pos++;
switch (c) {
case '\n':
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"lua read the final line part: \"%*s\"",
src->pos - 1 - begin, begin);
buf_in->buf->last = dst;
dd("read a line: %p: %.*s", buf_in,
(int) (buf_in->buf->last - buf_in->buf->pos), buf_in->buf->pos);
return NGX_OK;
case '\r':
/* ignore it */
break;
default:
*dst++ = c;
break;
}
}
#if (NGX_DEBUG)
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
"lua read partial line data: %*s", dst - begin, begin);
#endif
buf_in->buf->last = dst;
return NGX_AGAIN;
}