nginx-0.0.10-2004-09-09-22:55:39 import
Igor Sysoev
17 years ago
6 | 6 | |
7 | 7 | |
8 | 8 | static void ngx_imap_init_session(ngx_event_t *rev); |
9 | static void ngx_pop3_auth_state(ngx_event_t *rev); | |
10 | static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s); | |
9 | 11 | |
10 | 12 | |
11 | static char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF; | |
12 | static char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF; | |
13 | static u_char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF; | |
14 | static u_char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF; | |
15 | ||
16 | static u_char pop3_ok[] = "+OK" CRLF; | |
17 | static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF; | |
13 | 18 | |
14 | 19 | |
15 | 20 | void ngx_imap_init_connection(ngx_connection_t *c) |
16 | 21 | { |
17 | char *greeting; | |
18 | ssize_t size; | |
19 | ngx_int_t n; | |
22 | u_char *greeting; | |
23 | ssize_t size; | |
20 | 24 | |
21 | 25 | ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, |
22 | 26 | "imap init connection"); |
26 | 30 | greeting = pop3_greeting; |
27 | 31 | size = sizeof(pop3_greeting) - 1; |
28 | 32 | |
29 | n = ngx_send(c, greeting, size); | |
30 | ||
31 | if (n < size) { | |
33 | if (ngx_send(c, greeting, size) < size) { | |
32 | 34 | /* |
33 | 35 | * we treat the incomplete sending as NGX_ERROR |
34 | 36 | * because it is very strange here |
49 | 51 | |
50 | 52 | static void ngx_imap_init_session(ngx_event_t *rev) |
51 | 53 | { |
54 | size_t size; | |
52 | 55 | ngx_connection_t *c; |
53 | 56 | ngx_imap_session_t *s; |
54 | 57 | |
55 | 58 | c = rev->data; |
59 | ||
60 | /* TODO: timeout */ | |
56 | 61 | |
57 | 62 | if (!(s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)))) { |
58 | 63 | ngx_imap_close_connection(c); |
67 | 72 | return; |
68 | 73 | } |
69 | 74 | |
70 | s->buffer = ngx_create_temp_buf(s->connection->pool, /* STUB */ 4096); | |
75 | size = /* STUB: pop3: 128, imap: configurable 4K default */ 128; | |
76 | ||
77 | s->buffer = ngx_create_temp_buf(s->connection->pool, size); | |
71 | 78 | if (s->buffer == NULL) { |
72 | 79 | ngx_imap_close_connection(s->connection); |
73 | 80 | return; |
74 | 81 | } |
75 | 82 | |
76 | ngx_imap_proxy_init(s); | |
83 | ngx_pop3_auth_state(rev); | |
77 | 84 | } |
85 | ||
86 | ||
87 | static void ngx_pop3_auth_state(ngx_event_t *rev) | |
88 | { | |
89 | u_char *text; | |
90 | ssize_t size; | |
91 | ngx_int_t rc; | |
92 | ngx_connection_t *c; | |
93 | ngx_imap_session_t *s; | |
94 | ||
95 | c = rev->data; | |
96 | s = c->data; | |
97 | ||
98 | /* TODO: timeout */ | |
99 | ||
100 | rc = ngx_pop3_read_command(s); | |
101 | ||
102 | if (rc == NGX_AGAIN || rc == NGX_ERROR) { | |
103 | return; | |
104 | } | |
105 | ||
106 | s->state = 0; | |
107 | ||
108 | if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) { | |
109 | text = pop3_invalid_command; | |
110 | size = sizeof(pop3_invalid_command) - 1; | |
111 | ||
112 | } else { | |
113 | text = pop3_ok; | |
114 | size = sizeof(pop3_ok) - 1; | |
115 | } | |
116 | ||
117 | if (ngx_send(c, text, size) < size) { | |
118 | /* | |
119 | * we treat the incomplete sending as NGX_ERROR | |
120 | * because it is very strange here | |
121 | */ | |
122 | ngx_imap_close_connection(c); | |
123 | return; | |
124 | } | |
125 | } | |
126 | ||
127 | ||
128 | static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s) | |
129 | { | |
130 | ssize_t n; | |
131 | ngx_int_t rc; | |
132 | ||
133 | n = ngx_recv(s->connection, s->buffer->last, | |
134 | s->buffer->end - s->buffer->last); | |
135 | ||
136 | if (n == NGX_ERROR || n == 0) { | |
137 | ngx_imap_close_connection(s->connection); | |
138 | return NGX_ERROR; | |
139 | } | |
140 | ||
141 | if (n > 0) { | |
142 | s->buffer->last += n; | |
143 | } | |
144 | ||
145 | if (n == NGX_AGAIN) { | |
146 | if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) { | |
147 | ngx_imap_close_connection(s->connection); | |
148 | return NGX_ERROR; | |
149 | } | |
150 | ||
151 | return NGX_AGAIN; | |
152 | } | |
153 | ||
154 | rc = ngx_pop3_parse_command(s); | |
155 | ||
156 | if (rc == NGX_AGAIN || rc == NGX_IMAP_PARSE_INVALID_COMMAND) { | |
157 | return rc; | |
158 | } | |
159 | ||
160 | if (rc == NGX_ERROR) { | |
161 | ngx_imap_close_connection(s->connection); | |
162 | return NGX_ERROR; | |
163 | } | |
164 | ||
165 | return NGX_OK; | |
166 | } | |
167 | ||
168 | ||
169 | #if 0 | |
170 | ||
171 | void ngx_imap_close_session(ngx_imap_session_t *s) | |
172 | { | |
173 | ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, | |
174 | "close imap session"); | |
175 | ||
176 | ngx_imap_close_connection(s->connection); | |
177 | } | |
178 | ||
179 | #endif | |
78 | 180 | |
79 | 181 | |
80 | 182 | void ngx_imap_close_connection(ngx_connection_t *c) |