-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.asm
408 lines (337 loc) · 11.8 KB
/
httpd.asm
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
;*****************************************************************************;
; Copyright (C) 2023-2024, Mikhail Frolov aka Doczom . All rights reserved. ;
; Distributed under terms of the 3-Clause BSD License. ;
; ;
; httpd - Simple http server for Kolibri OS. ;
; ;
; Version 0.2.5, 13 June 2024 ;
; ;
;*****************************************************************************;
;include "macros.inc"
use32
org 0
db 'MENUET01'
dd 1, START, I_END, MEM, STACKTOP
M01header.params:
dd PATH, 0
include "macros.inc"
purge mov,add,sub
include 'module_api.inc'
include "proc32.inc"
include "dll.inc"
include "KOSfuncs.inc"
;include 'D:\kos\programs\network.inc'
;KOS_APP_START
include 'sys_func.inc'
include 'settings.inc'
;CODE
START:
mcall SF_SYS_MISC, SSF_HEAP_INIT ; init heap
mcall SF_SET_EVENTS_MASK, EVM_STACK ;set event bitmap
; init library
stdcall dll.Load, @IMPORT
test eax, eax
jnz .err_settings
mov ecx, default_ini_path
mov eax, [M01header.params]
cmp byte[eax],0
jz .load_settings
mov edx, ' '
mov ecx, eax
cmp byte[eax], '"'
jne @f
mov dl, '"'
inc ecx
@@:
mov esi, ecx
@@:
lodsb
test al, al
jz @f
cmp al, dl
jne @b
@@:
mov byte[esi - 1], 0
.load_settings:
; get settings
call load_settings ; ecx -> string to config file
test eax, eax
jnz .err_settings
;init server socket
push dword SO_NONBLOCK ;IPPROTO_TCP
push dword SOCK_STREAM
push dword AF_INET4
call netfunc_socket; AF_INET4, SOCK_STREAM, SO_NONBLOCK ; we dont want to block on accept
cmp eax, -1
je .sock_err
mov [srv_socket], eax
push srv_sockaddr.length
push dword srv_sockaddr
push dword[srv_socket]
call netfunc_bind; [srv_socket], srv_sockaddr, srv_sockaddr.length
cmp eax, -1
je .bind_err
; listen()
push dword[srv_backlog]
push dword[srv_socket]
call netfunc_listen; [srv_socket], [srv_backlog]
cmp eax, -1
jz .listen_err
.mainloop:
cmp dword[srv_shutdown], 1
jz .shutdown
mcall 23, 100 ; get event to network stack
test eax, eax
jz .mainloop
cmp dword[srv_stop], 1
jz .mainloop
push dword thread_connect
call CreateThread ; not save PID
jmp .mainloop
.shutdown:
.listen_err:
.bind_err:
push dword[srv_socket]
call netfunc_close; [srv_socket]
.err_settings:
.sock_err:
mcall SF_TERMINATE_PROCESS
;-----------------------------------------------------------------------------
thread_connect:
sub esp, sizeof.CONNECT_DATA
mcall SF_SET_EVENTS_MASK, EVM_STACK ; set event - network event
; ожидание подключения Accept, sockaddr находится на вершине стека нового потока
;lea edx, [esp + CONNECT_DATA.sockaddr] ; new sockaddr
;push dword 16 ; 16 byte - sockaddr length
;push edx
push srv_sockaddr.length
push dword srv_sockaddr
push dword[srv_socket]
call netfunc_accept
cmp eax, -1
jz .err_accept
mov [esp + CONNECT_DATA.socket], eax
mov ebp, esp
; соединение установленно, теперь нужно выделить буфер(тоже на 16 кб наверное), и
; прочитать в этот буфер из сокета, когда прочтём ноль(или больше 4 кб), тогда
; выходим из цикла и анализируем заголовки и стартовую стоку.
mov esi, 0x8000
push esi
call Alloc ;alloc memory 32 kib
test eax, eax
jz .err_alloc
mov [esp + CONNECT_DATA.buffer_request], eax
mov edx, eax
mov esi, esp ; SAVE CONNECT_DATA
mov dword[esi + CONNECT_DATA.request_size], 0
@@:
;read data from socket
push dword 0 ;flags
mov eax, [esi + CONNECT_DATA.request_size]
push dword 0x8000
sub [esp], eax
push dword[esi + CONNECT_DATA.buffer_request]
add [esp], eax
push dword[esi + CONNECT_DATA.socket]
call netfunc_recv
cmp eax, -1
jz .err_recv_sock
test eax, eax
jz @f
add [esi + CONNECT_DATA.request_size], eax
; cmp [esi + CONNECT_DATA.request_size], 0x8000 ; check end buffer
; jb @b
@@:
; после получения всего запроса(более или менее всего) выделяем озу для
; ассоциативного массива заголовков и аргументов запроса
; 8*50 + 8*100
; esp .. esp + 1024 -> for http headers
; esp + 1024 .. esp + 2048 -> for URI args
sub esp, 2048
; parse http message
mov ecx, [esi + CONNECT_DATA.buffer_request]
call parse_http_query ; ecx - buffer
test eax, eax
jz .err_parse
; find unit for uri path
cmp dword[GLOBAL_DATA.modules], 0
jz .no_modules
mov eax, [GLOBAL_DATA.modules]
.next_unit:
push esi edi
mov esi, [esi + CONNECT_DATA.uri_path]
lea edi, [eax + HTTPD_MODULE.uri_path]
; check module for all uri path
@@:
cmpsb
jne @f
cmp byte[edi - 1], 0
jne @b
.found_module:
; found module
pop edi esi
push dword[eax + HTTPD_MODULE.pdata] ; context of module
push esi ; coutext of request
call dword[eax + HTTPD_MODULE.httpd_serv] ; call unit function
jmp .end_work
@@:
.found_special_char:
cmp byte[edi - 1], '*'
jne .no_special_char
;je .found_module
; check next char in uri of modules
cmp byte[edi], 0
je .found_module
; uri path "*name" or "*name*"
dec esi
.special_char_loop:
cmp byte[esi], 0
je .no_special_char
;inc esi
xor ecx, ecx
@@:
inc ecx
mov dl, byte[edi + ecx - 1]
cmp dl, '*'
je @f
cmp byte[esi + ecx - 1], dl
lea esi, [esi + 1]
jne .special_char_loop
dec esi
test dl, dl
jnz @b
jmp .found_module
@@:
; found substr
add esi, ecx
add edi, ecx
jmp .found_special_char
.no_special_char:
pop edi esi
mov eax, [eax] ; HTTPD_MODULE.next
test eax, eax ; terminate list
jne .next_unit
.no_modules:
; if not found modules, call file_server
call file_server ; esi - struct
; end work thread
jmp .end_work
.err_parse:
call file_server.err_http_501
.end_work:
add esp, 2048
.err_recv_sock:
; free IN buffer
cmp dword[esp + CONNECT_DATA.buffer_request], 0
jz .err_alloc
push dword[esp + CONNECT_DATA.buffer_request]
call Free
.err_alloc:
push dword[esp + CONNECT_DATA.socket]
call netfunc_close
.err_accept:
lea ecx,[esp + sizeof.CONNECT_DATA - 0x4000] ; get pointer to alloc memory
mcall 68, 13 ; free
mcall SF_TERMINATE_PROCESS ; close thread
include 'parser.inc'
include 'file_server.inc'
; DATA AND FUNCTION
include 'httpd_lib.inc'
I_END:
;DATA
@IMPORT:
library libini, 'libini.obj'
import libini,\
ini.get_str, 'ini_get_str',\
ini.get_int, 'ini_get_int',\
ini.enum_keys, 'ini_enum_keys'
default_ini_path: db 'httpd.ini',0
ini_section_units: db 'MODULES',0
ini_section_main: db 'MAIN', 0
ini_section_tls db 'TLS',0
ini_key_ip db 'ip',0
ini_key_port db 'port',0
ini_key_conn db 'conn',0
ini_key_flags db 'flags',0
ini_key_work_dir db 'work_dir',0
ini_key_modules_dir db 'modules_dir',0
ini_key_mime_file db 'mime_file',0
ini_key_mbedtls_obj db 'mbedtls',0
ini_key_ca_cer db 'ca_cer',0
ini_key_srv_crt db 'srv_crt',0
ini_key_srv_key db 'srv_key',0
httpd_module_init db 'httpd_init',0
httpd_module_serv db 'httpd_serv',0
httpd_module_close db 'httpd_close',0
IMPORT_MODULE:
dd httpd_import, GLOBAL_DATA.modules_dir, 0
httpd_import:
.init dd httpd_module_init
.serv dd httpd_module_serv
.close dd httpd_module_close
dd 0
EXPORT_DATA: ; in modules for this table using struct IMPORT_DATA
dd API_VERSION
dd .size
dd netfunc_socket
dd netfunc_close
dd netfunc_bind
dd netfunc_accept
dd netfunc_listen
dd netfunc_recv
dd netfunc_send
dd Alloc
dd Free
dd parse_http_query ; no stdcall
dd FileInitFILED
dd FileInfo
dd FileRead
dd FileSetOffset
dd FileReadOfName
dd send_resp
dd create_resp
dd destruct_resp
dd set_http_status
dd add_http_header
dd del_http_header
dd set_http_ver
dd begin_send_resp
dd finish_send_resp
dd find_uri_arg
dd find_header
dd read_http_body
dd Get_MIME_Type
dd close_server
dd GLOBAL_DATA
.size = $ - EXPORT_DATA ; (count func)*4 + size(api ver) + 4
dd 0
; DATA
;UDATA
srv_stop: rd 1 ; set 1 for skip new connections
srv_shutdown: rd 1 ; set 1 for ending working server
srv_tls_enable rd 1 ; set 1 for enable TLS mode working
srv_backlog: rd 1 ; maximum number of simultaneous open connections
srv_socket: rd 1
srv_sockaddr:
rw 1
.port rw 1
.ip rd 1
rb 8
.length = $ - srv_sockaddr
GLOBAL_DATA:
.modules rd 1 ; pointer to a doubly connected non-cyclic list (null terminator)
; next, prev, ptr of httpd_serv(), uri path
.work_dir rb 1024 ; max size path to work directory
.work_dir.size rd 1 ; length string
.modules_dir rb 1024
.modules_dir.end rd 1
.MIME_types_arr rd 1
.flags rd 1
._module_cmd rd 1
PATH:
rb 256
; stack memory
rb 4096
STACKTOP:
MEM:
;KOS_APP_END