2013-11-04 17:07:21 +04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2017-06-11 13:50:58 +03:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2017. All rights reserved. ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; HTTP library for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
2013-11-15 11:49:31 +04:00
|
|
|
;; Proxy code written by CleverMouse ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
; references:
|
|
|
|
; "HTTP made really easy", http://www.jmarshall.com/easy/http/
|
|
|
|
; "Hypertext Transfer Protocol -- HTTP/1.1", http://tools.ietf.org/html/rfc2616
|
|
|
|
|
|
|
|
|
|
|
|
URLMAXLEN = 65535
|
2014-01-28 01:15:30 +04:00
|
|
|
BUFFERSIZE = 8192
|
2014-03-23 23:37:44 +04:00
|
|
|
TIMEOUT = 500 ; in 1/100 s
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
__DEBUG__ = 1
|
2015-08-17 13:33:23 +03:00
|
|
|
__DEBUG_LEVEL__ = 2
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
format MS COFF
|
|
|
|
|
|
|
|
public @EXPORT as 'EXPORTS'
|
|
|
|
|
|
|
|
include '../../../struct.inc'
|
|
|
|
include '../../../proc32.inc'
|
|
|
|
include '../../../macros.inc'
|
|
|
|
purge section,mov,add,sub
|
|
|
|
include '../../../debug-fdo.inc'
|
|
|
|
|
|
|
|
include '../../../network.inc'
|
|
|
|
include 'http.inc'
|
|
|
|
|
|
|
|
virtual at 0
|
|
|
|
http_msg http_msg
|
|
|
|
end virtual
|
|
|
|
|
|
|
|
macro copy_till_zero {
|
2013-11-17 00:20:35 +04:00
|
|
|
local .copyloop, .copydone
|
|
|
|
.copyloop:
|
2013-11-04 17:07:21 +04:00
|
|
|
lodsb
|
|
|
|
test al, al
|
2013-11-17 00:20:35 +04:00
|
|
|
jz .copydone
|
2013-11-04 17:07:21 +04:00
|
|
|
stosb
|
2013-11-17 00:20:35 +04:00
|
|
|
jmp .copyloop
|
|
|
|
.copydone:
|
2013-11-04 17:07:21 +04:00
|
|
|
}
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
macro HTTP_init_buffer buffer, socketnum, flags {
|
2013-11-05 15:22:31 +04:00
|
|
|
|
|
|
|
mov eax, buffer
|
|
|
|
push socketnum
|
|
|
|
popd [eax + http_msg.socket]
|
2014-01-28 01:15:30 +04:00
|
|
|
lea esi, [eax + http_msg.http_header]
|
2015-03-20 15:04:14 +03:00
|
|
|
push flags
|
|
|
|
pop [eax + http_msg.flags]
|
|
|
|
or [eax + http_msg.flags], FLAG_CONNECTED
|
2013-11-05 15:22:31 +04:00
|
|
|
mov [eax + http_msg.write_ptr], esi
|
2014-01-28 01:15:30 +04:00
|
|
|
mov [eax + http_msg.buffer_length], BUFFERSIZE - http_msg.http_header
|
2013-11-05 15:22:31 +04:00
|
|
|
mov [eax + http_msg.chunk_ptr], 0
|
|
|
|
|
|
|
|
mov [eax + http_msg.status], 0
|
|
|
|
mov [eax + http_msg.header_length], 0
|
2014-01-28 01:15:30 +04:00
|
|
|
mov [eax + http_msg.content_ptr], 0
|
2013-11-05 15:22:31 +04:00
|
|
|
mov [eax + http_msg.content_length], 0
|
2013-11-05 21:02:59 +04:00
|
|
|
mov [eax + http_msg.content_received], 0
|
2013-11-11 16:29:54 +04:00
|
|
|
|
|
|
|
push eax ebp
|
|
|
|
mov ebp, eax
|
2014-03-23 23:37:44 +04:00
|
|
|
mcall 26, 9
|
2013-11-11 16:29:54 +04:00
|
|
|
mov [ebp + http_msg.timestamp], eax
|
|
|
|
pop ebp eax
|
2013-11-05 15:22:31 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
section '.flat' code readable align 16
|
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
lib_init: ;//////////////////////////////////////////////////////////////////;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;? Library entry point (called after library load) ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
|
|
|
;> eax = pointer to memory allocation routine ;;
|
|
|
|
;> ebx = pointer to memory freeing routine ;;
|
|
|
|
;> ecx = pointer to memory reallocation routine ;;
|
|
|
|
;> edx = pointer to library loading routine ;;
|
|
|
|
;;---------------------------------------------------------------------------;;
|
2013-11-11 21:28:49 +04:00
|
|
|
;< eax = 1 (fail) / 0 (ok) ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;===========================================================================;;
|
|
|
|
mov [mem.alloc], eax
|
|
|
|
mov [mem.free], ebx
|
|
|
|
mov [mem.realloc], ecx
|
|
|
|
mov [dll.load], edx
|
|
|
|
|
|
|
|
invoke dll.load, @IMPORT
|
2013-11-11 21:28:49 +04:00
|
|
|
test eax, eax
|
|
|
|
jnz .error
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
; load proxy settings
|
2013-11-12 14:37:17 +04:00
|
|
|
pusha
|
2013-11-04 17:07:21 +04:00
|
|
|
invoke ini.get_str, inifile, sec_proxy, key_proxy, proxyAddr, 256, proxyAddr
|
|
|
|
invoke ini.get_int, inifile, sec_proxy, key_proxyport, 80
|
|
|
|
mov [proxyPort], eax
|
|
|
|
invoke ini.get_str, inifile, sec_proxy, key_user, proxyUser, 256, proxyUser
|
|
|
|
invoke ini.get_str, inifile, sec_proxy, key_password, proxyPassword, 256, proxyPassword
|
2013-11-12 14:37:17 +04:00
|
|
|
popa
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-12 01:25:11 +04:00
|
|
|
DEBUGF 1, "HTTP library: init OK\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
.error:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR loading http.obj dependencies\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
xor eax, eax
|
2013-11-11 21:28:49 +04:00
|
|
|
inc eax
|
|
|
|
ret
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
|
2014-07-16 18:11:19 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
proc HTTP_disconnect identifier ;/////////////////////////////////////////////////////////////////;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? Stops the open connection ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> identifier = pointer to buffer containing http_msg struct. ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< none ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
|
|
|
|
pusha
|
|
|
|
mov ebp, [identifier]
|
|
|
|
|
|
|
|
test [ebp + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .error
|
|
|
|
and [ebp + http_msg.flags], not FLAG_CONNECTED
|
|
|
|
mcall close, [ebp + http_msg.socket]
|
|
|
|
|
|
|
|
popa
|
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2017-09-24 00:27:30 +03:00
|
|
|
DEBUGF 1, "Cannot close already closed connection!\n"
|
2014-07-16 18:11:19 +04:00
|
|
|
popa
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
|
2014-07-16 18:11:19 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
proc HTTP_free identifier ;///////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? Free the http_msg structure ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> identifier = pointer to buffer containing http_msg struct. ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< none ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
DEBUGF 1, "HTTP_free: 0x%x\n", [identifier]
|
|
|
|
pusha
|
|
|
|
mov ebp, [identifier]
|
|
|
|
|
|
|
|
test [ebp + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .not_connected
|
|
|
|
and [ebp + http_msg.flags], not FLAG_CONNECTED
|
|
|
|
mcall close, [ebp + http_msg.socket]
|
|
|
|
|
|
|
|
.not_connected:
|
|
|
|
invoke mem.free, ebp
|
|
|
|
|
|
|
|
popa
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
;;================================================================================================;;
|
2015-03-20 15:04:14 +03:00
|
|
|
proc HTTP_get URL, identifier, flags, add_header ;////////////////////////////////////////////////;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;? Initiates a HTTP connection, using 'GET' method. ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2015-03-20 15:04:14 +03:00
|
|
|
;> URL = pointer to ASCIIZ URL ;;
|
|
|
|
;> identifier = Identifier of an already open connection, or NULL to create a new one. ;;
|
|
|
|
;> flags = Flags indicating how to threat the connection. ;;
|
|
|
|
;> add_header = pointer to additional header parameters (ASCIIZ), or NULL for none. ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< eax = 0 (error) / buffer ptr ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
locals
|
|
|
|
hostname dd ?
|
|
|
|
pageaddr dd ?
|
|
|
|
socketnum dd ?
|
|
|
|
buffer dd ?
|
2013-11-05 15:22:31 +04:00
|
|
|
port dd ?
|
2013-11-04 17:07:21 +04:00
|
|
|
endl
|
|
|
|
|
2015-03-20 17:00:21 +03:00
|
|
|
and [flags], 0xff00 ; filter out invalid flags
|
2015-03-20 15:04:14 +03:00
|
|
|
|
2013-11-12 14:37:17 +04:00
|
|
|
pusha
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
; split the URL into hostname and pageaddr
|
|
|
|
stdcall parse_url, [URL]
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
mov [hostname], eax
|
|
|
|
mov [pageaddr], ebx
|
2013-11-13 22:25:47 +04:00
|
|
|
mov [port], ecx
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
mov eax, [identifier]
|
|
|
|
test eax, eax
|
|
|
|
jz .open_new
|
|
|
|
test [eax + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .error
|
|
|
|
mov eax, [eax + http_msg.socket]
|
|
|
|
mov [socketnum], eax
|
|
|
|
jmp .send_request
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Connect to the other side.
|
2015-03-20 15:04:14 +03:00
|
|
|
.open_new:
|
2013-11-05 15:22:31 +04:00
|
|
|
stdcall open_connection, [hostname], [port]
|
2013-11-04 17:07:21 +04:00
|
|
|
test eax, eax
|
2013-11-05 15:22:31 +04:00
|
|
|
jz .error
|
|
|
|
mov [socketnum], eax
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Create the HTTP request.
|
2015-03-20 15:04:14 +03:00
|
|
|
.send_request:
|
2013-11-05 15:22:31 +04:00
|
|
|
invoke mem.alloc, BUFFERSIZE
|
2013-11-04 17:07:21 +04:00
|
|
|
test eax, eax
|
|
|
|
jz .error
|
2013-11-05 15:22:31 +04:00
|
|
|
mov [buffer], eax
|
|
|
|
mov edi, eax
|
2014-01-28 01:15:30 +04:00
|
|
|
DEBUGF 1, "Buffer allocated: 0x%x\n", eax
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, str_get
|
|
|
|
copy_till_zero
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
; If we are using a proxy, send complete URL, otherwise send only page address.
|
|
|
|
cmp [proxyAddr], 0
|
|
|
|
je .no_proxy
|
|
|
|
mov esi, str_http ; prepend 'http://'
|
|
|
|
copy_till_zero
|
|
|
|
mov esi, [hostname]
|
|
|
|
copy_till_zero
|
|
|
|
.no_proxy:
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, [pageaddr]
|
|
|
|
copy_till_zero
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, str_http11
|
|
|
|
mov ecx, str_http11.length
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov esi, [hostname]
|
|
|
|
copy_till_zero
|
|
|
|
|
2013-11-17 00:20:35 +04:00
|
|
|
cmp byte[proxyUser], 0
|
|
|
|
je @f
|
|
|
|
call append_proxy_auth_header
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov ax, 0x0a0d
|
|
|
|
stosw
|
|
|
|
|
2013-11-13 22:25:47 +04:00
|
|
|
mov esi, [add_header]
|
|
|
|
test esi, esi
|
|
|
|
jz @f
|
|
|
|
copy_till_zero
|
|
|
|
@@:
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, str_close
|
|
|
|
mov ecx, str_close.length
|
2015-03-20 15:04:14 +03:00
|
|
|
test [flags], FLAG_KEEPALIVE
|
|
|
|
jz @f
|
|
|
|
mov esi, str_keep
|
|
|
|
mov ecx, str_keep.length
|
|
|
|
@@:
|
2013-11-05 15:22:31 +04:00
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov byte[edi], 0
|
|
|
|
DEBUGF 1, "Request:\n%s", [buffer]
|
|
|
|
|
2013-11-13 23:21:57 +04:00
|
|
|
; Free unused memory
|
|
|
|
push edi
|
|
|
|
invoke mem.free, [pageaddr]
|
|
|
|
invoke mem.free, [hostname]
|
|
|
|
pop esi
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Send the request
|
|
|
|
sub esi, [buffer] ; length
|
|
|
|
xor edi, edi ; flags
|
|
|
|
mcall send, [socketnum], [buffer]
|
2013-11-04 17:07:21 +04:00
|
|
|
test eax, eax
|
|
|
|
jz .error
|
2013-11-05 15:22:31 +04:00
|
|
|
DEBUGF 1, "Request has been sent to server.\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
cmp [identifier], 0
|
2015-08-24 16:43:34 +03:00
|
|
|
je .new_connection
|
|
|
|
invoke mem.free, [buffer]
|
|
|
|
mov eax, [identifier]
|
|
|
|
mov [buffer], eax
|
|
|
|
.new_connection:
|
2015-03-20 15:04:14 +03:00
|
|
|
HTTP_init_buffer [buffer], [socketnum], [flags]
|
2013-11-12 14:37:17 +04:00
|
|
|
popa
|
|
|
|
mov eax, [buffer] ; return buffer ptr
|
|
|
|
ret
|
2013-11-05 15:22:31 +04:00
|
|
|
|
|
|
|
.error:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "HTTP GET error!\n"
|
2013-11-12 14:37:17 +04:00
|
|
|
popa
|
2013-11-05 15:22:31 +04:00
|
|
|
xor eax, eax ; return 0 = error
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;================================================================================================;;
|
2015-03-20 15:04:14 +03:00
|
|
|
proc HTTP_head URL, identifier, flags, add_header ;///////////////////////////////////////////////;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;? Initiates a HTTP connection, using 'HEAD' method. ;;
|
2014-01-28 01:15:30 +04:00
|
|
|
;? This will only return HTTP header and status, no content ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2015-03-20 15:04:14 +03:00
|
|
|
;> URL = pointer to ASCIIZ URL ;;
|
|
|
|
;> identifier = Identifier of an already open connection, or NULL to create a new one. ;;
|
|
|
|
;> flags = Flags indicating how to threat the connection. ;;
|
|
|
|
;> add_header = pointer to additional header parameters (ASCIIZ), or NULL for none. ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< eax = 0 (error) / buffer ptr ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
locals
|
|
|
|
hostname dd ?
|
|
|
|
pageaddr dd ?
|
|
|
|
socketnum dd ?
|
|
|
|
buffer dd ?
|
|
|
|
port dd ?
|
|
|
|
endl
|
|
|
|
|
2015-03-20 17:00:21 +03:00
|
|
|
and [flags], 0xff00 ; filter out invalid flags
|
2015-03-20 15:04:14 +03:00
|
|
|
|
2013-11-12 14:37:17 +04:00
|
|
|
pusha
|
2013-11-05 15:22:31 +04:00
|
|
|
; split the URL into hostname and pageaddr
|
|
|
|
stdcall parse_url, [URL]
|
2013-11-04 17:07:21 +04:00
|
|
|
test eax, eax
|
2013-11-05 15:22:31 +04:00
|
|
|
jz .error
|
|
|
|
mov [hostname], eax
|
|
|
|
mov [pageaddr], ebx
|
2013-11-13 22:25:47 +04:00
|
|
|
mov [port], ecx
|
2013-11-05 15:22:31 +04:00
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
mov eax, [identifier]
|
|
|
|
test eax, eax
|
|
|
|
jz .open_new
|
|
|
|
test [eax + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .error
|
|
|
|
mov eax, [eax + http_msg.socket]
|
|
|
|
mov [socketnum], eax
|
|
|
|
jmp .send_request
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Connect to the other side.
|
2015-03-20 15:04:14 +03:00
|
|
|
.open_new:
|
2013-11-05 15:22:31 +04:00
|
|
|
stdcall open_connection, [hostname], [port]
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
mov [socketnum], eax
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
; Create the HTTP request.
|
2015-03-20 15:04:14 +03:00
|
|
|
.send_request:
|
2013-11-04 17:07:21 +04:00
|
|
|
invoke mem.alloc, BUFFERSIZE
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
mov [buffer], eax
|
2013-11-05 15:22:31 +04:00
|
|
|
mov edi, eax
|
2013-11-04 17:07:21 +04:00
|
|
|
DEBUGF 1, "Buffer has been allocated.\n"
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, str_head
|
|
|
|
copy_till_zero
|
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
; If we are using a proxy, send complete URL, otherwise send only page address.
|
|
|
|
cmp [proxyAddr], 0
|
|
|
|
je .no_proxy
|
|
|
|
mov esi, str_http ; prepend 'http://'
|
|
|
|
copy_till_zero
|
|
|
|
mov esi, [hostname]
|
|
|
|
copy_till_zero
|
|
|
|
.no_proxy:
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, [pageaddr]
|
2013-11-04 17:07:21 +04:00
|
|
|
copy_till_zero
|
|
|
|
|
|
|
|
mov esi, str_http11
|
|
|
|
mov ecx, str_http11.length
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov esi, [hostname]
|
|
|
|
copy_till_zero
|
|
|
|
|
2013-11-17 00:20:35 +04:00
|
|
|
cmp byte[proxyUser], 0
|
|
|
|
je @f
|
|
|
|
call append_proxy_auth_header
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov ax, 0x0a0d
|
|
|
|
stosw
|
|
|
|
|
2013-11-13 22:25:47 +04:00
|
|
|
mov esi, [add_header]
|
|
|
|
test esi, esi
|
|
|
|
jz @f
|
|
|
|
copy_till_zero
|
|
|
|
@@:
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
mov esi, str_close
|
|
|
|
mov ecx, str_close.length
|
2015-03-20 15:04:14 +03:00
|
|
|
test [flags], FLAG_KEEPALIVE
|
|
|
|
jz @f
|
|
|
|
mov esi, str_keep
|
|
|
|
mov ecx, str_keep.length
|
|
|
|
@@:
|
2013-11-04 17:07:21 +04:00
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov byte[edi], 0
|
|
|
|
DEBUGF 1, "Request:\n%s", [buffer]
|
|
|
|
|
2013-11-13 23:21:57 +04:00
|
|
|
; Free unused memory
|
|
|
|
push edi
|
|
|
|
invoke mem.free, [pageaddr]
|
|
|
|
invoke mem.free, [hostname]
|
|
|
|
pop esi
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Send the request
|
2013-11-04 17:07:21 +04:00
|
|
|
sub esi, [buffer] ; length
|
|
|
|
xor edi, edi ; flags
|
|
|
|
mcall send, [socketnum], [buffer]
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
DEBUGF 1, "Request has been sent to server.\n"
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
cmp [identifier], 0
|
2015-08-24 16:43:34 +03:00
|
|
|
je .new_connection
|
|
|
|
invoke mem.free, [buffer]
|
|
|
|
mov eax, [identifier]
|
|
|
|
mov [buffer], eax
|
|
|
|
.new_connection:
|
2015-03-20 15:04:14 +03:00
|
|
|
HTTP_init_buffer [buffer], [socketnum], [flags]
|
2013-11-12 14:37:17 +04:00
|
|
|
popa
|
2015-03-20 15:04:14 +03:00
|
|
|
mov eax, [buffer] ; return buffer ptr
|
|
|
|
ret
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
.error:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "HTTP HEAD error!\n"
|
2013-11-12 14:37:17 +04:00
|
|
|
popa
|
2013-11-05 15:22:31 +04:00
|
|
|
xor eax, eax ; return 0 = error
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;;================================================================================================;;
|
2015-03-20 15:04:14 +03:00
|
|
|
proc HTTP_post URL, identifier, flags, add_header, content_type, content_length ;/////////////////;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2014-01-28 01:15:30 +04:00
|
|
|
;? Initiates a HTTP connection, using 'POST' method. ;;
|
|
|
|
;? This method is used to send data to the HTTP server ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;> URL = pointer to ASCIIZ URL ;;
|
2015-03-20 15:04:14 +03:00
|
|
|
;> identifier = Identifier of an already open connection, or NULL to create a new one. ;;
|
|
|
|
;> flags = Flags indicating how to threat the connection. ;;
|
|
|
|
;> add_header = pointer to additional header parameters (ASCIIZ), or NULL for none. ;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;> content_type = pointer to ASCIIZ string containing content type ;;
|
|
|
|
;> content_length = length of content (in bytes) ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2015-03-20 15:04:14 +03:00
|
|
|
;< eax = 0 (error) / buffer ptr (aka Identifier) ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
locals
|
|
|
|
hostname dd ?
|
|
|
|
pageaddr dd ?
|
|
|
|
socketnum dd ?
|
|
|
|
buffer dd ?
|
|
|
|
port dd ?
|
|
|
|
endl
|
|
|
|
|
2015-03-20 17:00:21 +03:00
|
|
|
and [flags], 0xff00 ; filter out invalid flags
|
2015-03-20 15:04:14 +03:00
|
|
|
|
2013-11-12 14:37:17 +04:00
|
|
|
pusha
|
2013-11-05 15:22:31 +04:00
|
|
|
; split the URL into hostname and pageaddr
|
|
|
|
stdcall parse_url, [URL]
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
mov [hostname], eax
|
|
|
|
mov [pageaddr], ebx
|
2013-11-13 22:25:47 +04:00
|
|
|
mov [port], ecx
|
2013-11-05 15:22:31 +04:00
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
mov eax, [identifier]
|
|
|
|
test eax, eax
|
|
|
|
jz .open_new
|
|
|
|
test [eax + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .error
|
|
|
|
mov eax, [eax + http_msg.socket]
|
|
|
|
mov [socketnum], eax
|
|
|
|
jmp .send_request
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Connect to the other side.
|
2015-03-20 15:04:14 +03:00
|
|
|
.open_new:
|
2013-11-05 15:22:31 +04:00
|
|
|
stdcall open_connection, [hostname], [port]
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
mov [socketnum], eax
|
|
|
|
|
|
|
|
; Create the HTTP request.
|
2015-03-20 15:04:14 +03:00
|
|
|
.send_request:
|
2013-11-05 15:22:31 +04:00
|
|
|
invoke mem.alloc, BUFFERSIZE
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
mov [buffer], eax
|
|
|
|
mov edi, eax
|
|
|
|
DEBUGF 1, "Buffer has been allocated.\n"
|
|
|
|
|
|
|
|
mov esi, str_post
|
|
|
|
copy_till_zero
|
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
; If we are using a proxy, send complete URL, otherwise send only page address.
|
|
|
|
cmp [proxyAddr], 0
|
|
|
|
je .no_proxy
|
|
|
|
mov esi, str_http ; prepend 'http://'
|
|
|
|
copy_till_zero
|
|
|
|
mov esi, [hostname]
|
|
|
|
copy_till_zero
|
|
|
|
.no_proxy:
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, [pageaddr]
|
|
|
|
copy_till_zero
|
|
|
|
|
|
|
|
mov esi, str_http11
|
|
|
|
mov ecx, str_http11.length
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov esi, [hostname]
|
|
|
|
copy_till_zero
|
|
|
|
|
|
|
|
mov esi, str_post_cl
|
|
|
|
mov ecx, str_post_cl.length
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov eax, [content_length]
|
2013-11-15 11:49:31 +04:00
|
|
|
call eax_ascii_dec
|
2013-11-05 15:22:31 +04:00
|
|
|
|
|
|
|
mov esi, str_post_ct
|
|
|
|
mov ecx, str_post_ct.length
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov esi, [content_type]
|
2013-11-13 22:25:47 +04:00
|
|
|
copy_till_zero
|
|
|
|
|
2013-11-17 00:20:35 +04:00
|
|
|
cmp byte[proxyUser], 0
|
|
|
|
je @f
|
|
|
|
call append_proxy_auth_header
|
|
|
|
@@:
|
|
|
|
|
|
|
|
mov ax, 0x0a0d
|
|
|
|
stosw
|
|
|
|
|
2013-11-13 22:25:47 +04:00
|
|
|
mov esi, [add_header]
|
|
|
|
test esi, esi
|
|
|
|
jz @f
|
|
|
|
copy_till_zero
|
|
|
|
@@:
|
2013-11-05 15:22:31 +04:00
|
|
|
|
|
|
|
mov esi, str_close
|
|
|
|
mov ecx, str_close.length
|
2015-03-20 15:04:14 +03:00
|
|
|
test [flags], FLAG_KEEPALIVE
|
|
|
|
jz @f
|
|
|
|
mov esi, str_keep
|
|
|
|
mov ecx, str_keep.length
|
|
|
|
@@:
|
2013-11-05 15:22:31 +04:00
|
|
|
rep movsb
|
|
|
|
|
|
|
|
mov byte[edi], 0
|
|
|
|
DEBUGF 1, "Request:\n%s", [buffer]
|
|
|
|
|
2013-11-13 23:21:57 +04:00
|
|
|
; Free unused memory
|
|
|
|
push edi
|
|
|
|
invoke mem.free, [pageaddr]
|
|
|
|
invoke mem.free, [hostname]
|
|
|
|
pop esi
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Send the request
|
|
|
|
sub esi, [buffer] ; length
|
|
|
|
xor edi, edi ; flags
|
|
|
|
mcall send, [socketnum], [buffer]
|
|
|
|
test eax, eax
|
|
|
|
jz .error
|
|
|
|
DEBUGF 1, "Request has been sent to server.\n"
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
cmp [identifier], 0
|
2015-08-24 16:43:34 +03:00
|
|
|
je .new_connection
|
2015-03-20 15:04:14 +03:00
|
|
|
invoke mem.free, [buffer]
|
|
|
|
mov eax, [identifier]
|
2015-08-24 16:43:34 +03:00
|
|
|
mov [buffer], eax
|
|
|
|
.new_connection:
|
|
|
|
HTTP_init_buffer [buffer], [socketnum], [flags]
|
2015-03-20 15:04:14 +03:00
|
|
|
popa
|
2015-08-24 16:43:34 +03:00
|
|
|
mov eax, [buffer] ; return buffer ptr
|
2015-03-20 15:04:14 +03:00
|
|
|
ret
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
.error:
|
2017-09-24 00:27:30 +03:00
|
|
|
DEBUGF 2, "HTTP POST error!\n"
|
2013-11-12 14:37:17 +04:00
|
|
|
popa
|
2013-11-04 17:07:21 +04:00
|
|
|
xor eax, eax ; return 0 = error
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;================================================================================================;;
|
2014-07-16 18:11:19 +04:00
|
|
|
proc HTTP_receive identifier ;////////////////////////////////////////////////////////////////////;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2014-07-16 18:11:19 +04:00
|
|
|
;? Receive data from the server, parse headers and put data in receive buffer(s). ;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;? To complete a transfer, this procedure must be called over and over again untill it returns 0. ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;> identifier = pointer to buffer containing http_msg struct. ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< eax = -1 (not finished) / 0 finished ;;
|
|
|
|
;;================================================================================================;;
|
2014-03-23 23:37:44 +04:00
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
pusha
|
|
|
|
mov ebp, [identifier]
|
2013-11-04 22:41:15 +04:00
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
; If the connection is closed, return immediately
|
2013-11-11 15:35:18 +04:00
|
|
|
test [ebp + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .connection_closed
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
; If the buffer is full, allocate a new one
|
|
|
|
cmp [ebp + http_msg.buffer_length], 0
|
|
|
|
jne .receive
|
|
|
|
|
2015-03-20 17:00:21 +03:00
|
|
|
test [ebp + http_msg.flags], FLAG_STREAM
|
2015-03-20 15:04:14 +03:00
|
|
|
jz .err_header
|
|
|
|
|
2015-03-20 17:00:21 +03:00
|
|
|
test [ebp + http_msg.flags], FLAG_REUSE_BUFFER
|
|
|
|
jz .new_buffer
|
|
|
|
|
|
|
|
mov eax, [ebp + http_msg.content_ptr]
|
|
|
|
mov [ebp + http_msg.write_ptr], eax
|
|
|
|
mov [ebp + http_msg.buffer_length], BUFFERSIZE
|
|
|
|
jmp .receive
|
|
|
|
|
|
|
|
.new_buffer:
|
2015-03-20 15:04:14 +03:00
|
|
|
invoke mem.alloc, BUFFERSIZE
|
|
|
|
test eax, eax
|
|
|
|
jz .err_no_ram
|
|
|
|
mov [ebp + http_msg.content_ptr], eax
|
|
|
|
mov [ebp + http_msg.write_ptr], eax
|
|
|
|
mov [ebp + http_msg.buffer_length], BUFFERSIZE
|
2015-03-20 17:00:21 +03:00
|
|
|
DEBUGF 1, "New buffer: 0x%x\n", eax
|
2015-03-20 15:04:14 +03:00
|
|
|
|
2013-11-04 22:41:15 +04:00
|
|
|
; Receive some data
|
2015-03-20 15:04:14 +03:00
|
|
|
.receive:
|
2015-08-17 13:33:23 +03:00
|
|
|
mov edi, MSG_DONTWAIT
|
|
|
|
test [ebp + http_msg.flags], FLAG_BLOCK
|
|
|
|
jz @f
|
|
|
|
xor edi, edi
|
|
|
|
@@:
|
2013-11-04 17:07:21 +04:00
|
|
|
mcall recv, [ebp + http_msg.socket], [ebp + http_msg.write_ptr], \
|
2015-08-17 13:33:23 +03:00
|
|
|
[ebp + http_msg.buffer_length]
|
2013-11-04 17:07:21 +04:00
|
|
|
cmp eax, 0xffffffff
|
|
|
|
je .check_socket
|
2013-11-13 00:08:02 +04:00
|
|
|
|
|
|
|
test eax, eax
|
|
|
|
jz .server_closed
|
2013-11-04 17:07:21 +04:00
|
|
|
DEBUGF 1, "Received %u bytes\n", eax
|
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
; Update timestamp
|
2013-11-11 16:29:54 +04:00
|
|
|
push eax
|
2014-03-23 23:37:44 +04:00
|
|
|
mcall 26, 9
|
2013-11-11 16:29:54 +04:00
|
|
|
mov [ebp + http_msg.timestamp], eax
|
|
|
|
pop eax
|
|
|
|
|
2013-11-04 22:41:15 +04:00
|
|
|
; Update pointers
|
2013-11-04 17:07:21 +04:00
|
|
|
mov edi, [ebp + http_msg.write_ptr]
|
|
|
|
add [ebp + http_msg.write_ptr], eax
|
|
|
|
sub [ebp + http_msg.buffer_length], eax
|
2013-11-04 22:41:15 +04:00
|
|
|
|
|
|
|
; If data is chunked, combine chunks into contiguous data.
|
|
|
|
test [ebp + http_msg.flags], FLAG_CHUNKED
|
|
|
|
jnz .chunk_loop
|
|
|
|
|
2013-11-12 01:25:11 +04:00
|
|
|
; Did we detect the (final) header yet?
|
2013-11-04 17:07:21 +04:00
|
|
|
test [ebp + http_msg.flags], FLAG_GOT_HEADER
|
|
|
|
jnz .header_parsed
|
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
;--------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Header parsing code begins here
|
|
|
|
;
|
|
|
|
|
2013-11-12 01:25:11 +04:00
|
|
|
; We havent found the (final) header yet, search for it..
|
|
|
|
.scan_again:
|
|
|
|
; eax = total number of bytes received so far
|
|
|
|
mov eax, [ebp + http_msg.write_ptr]
|
2014-01-28 01:15:30 +04:00
|
|
|
sub eax, http_msg.http_header
|
2013-11-12 01:25:11 +04:00
|
|
|
sub eax, ebp
|
|
|
|
sub eax, [ebp + http_msg.header_length]
|
|
|
|
; edi is ptr to begin of header
|
2014-01-28 01:15:30 +04:00
|
|
|
lea edi, [ebp + http_msg.http_header]
|
2013-11-12 01:25:11 +04:00
|
|
|
add edi, [ebp + http_msg.header_length]
|
|
|
|
; put it in esi for next proc too
|
|
|
|
mov esi, edi
|
|
|
|
sub eax, 3
|
2014-03-23 23:37:44 +04:00
|
|
|
jle .need_more_data_for_header
|
2013-11-12 01:25:11 +04:00
|
|
|
.scan_loop:
|
2013-11-04 17:07:21 +04:00
|
|
|
; scan for end of header (empty line)
|
|
|
|
cmp dword[edi], 0x0a0d0a0d ; end of header
|
|
|
|
je .end_of_header
|
2013-11-12 01:25:11 +04:00
|
|
|
cmp word[edi+2], 0x0a0a ; notice the use of offset + 2, to calculate header length correctly :)
|
2013-11-04 17:07:21 +04:00
|
|
|
je .end_of_header
|
|
|
|
inc edi
|
|
|
|
dec eax
|
2013-11-12 01:25:11 +04:00
|
|
|
jnz .scan_loop
|
2014-03-23 23:37:44 +04:00
|
|
|
jmp .need_more_data_for_header
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
.end_of_header:
|
2014-01-28 01:15:30 +04:00
|
|
|
add edi, 4 - http_msg.http_header
|
2013-11-04 17:07:21 +04:00
|
|
|
sub edi, ebp
|
2013-11-12 01:25:11 +04:00
|
|
|
mov [ebp + http_msg.header_length], edi ; If this isnt the final header, we'll use this as an offset to find real header.
|
2013-11-04 17:07:21 +04:00
|
|
|
DEBUGF 1, "Header length: %u\n", edi
|
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
; Ok, we have found the header
|
2013-11-12 01:25:11 +04:00
|
|
|
cmp dword[esi], 'HTTP'
|
2014-03-23 23:37:44 +04:00
|
|
|
jne .err_header
|
2013-11-12 01:25:11 +04:00
|
|
|
cmp dword[esi+4], '/1.0'
|
2013-11-04 17:07:21 +04:00
|
|
|
je .http_1.0
|
2013-11-12 01:25:11 +04:00
|
|
|
cmp dword[esi+4], '/1.1'
|
2014-03-23 23:37:44 +04:00
|
|
|
jne .err_header
|
2013-11-04 17:07:21 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_HTTP11
|
|
|
|
.http_1.0:
|
2013-11-12 01:25:11 +04:00
|
|
|
cmp byte[esi+8], ' '
|
2014-03-23 23:37:44 +04:00
|
|
|
jne .err_header
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-12 01:25:11 +04:00
|
|
|
add esi, 9
|
2013-11-04 17:07:21 +04:00
|
|
|
xor eax, eax
|
|
|
|
xor ebx, ebx
|
|
|
|
mov ecx, 3
|
|
|
|
.statusloop:
|
|
|
|
lodsb
|
|
|
|
sub al, '0'
|
2014-03-23 23:37:44 +04:00
|
|
|
jb .err_header
|
2013-11-04 17:07:21 +04:00
|
|
|
cmp al, 9
|
2014-03-23 23:37:44 +04:00
|
|
|
ja .err_header
|
2013-11-04 17:07:21 +04:00
|
|
|
lea ebx, [ebx + 4*ebx]
|
|
|
|
shl ebx, 1
|
|
|
|
add ebx, eax
|
|
|
|
dec ecx
|
|
|
|
jnz .statusloop
|
2013-11-12 01:25:11 +04:00
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
; Ignore "100 - Continue" lines
|
2013-11-12 01:25:11 +04:00
|
|
|
cmp ebx, 100
|
|
|
|
je .scan_again
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
DEBUGF 1, "Status: %u\n", ebx
|
2013-11-12 01:25:11 +04:00
|
|
|
mov [ebp + http_msg.status], ebx
|
|
|
|
or [ebp + http_msg.flags], FLAG_GOT_HEADER
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
; Now, convert all header names to lowercase.
|
|
|
|
; This way, it will be much easier to find certain header fields, later on.
|
2014-01-28 01:15:30 +04:00
|
|
|
lea esi, [ebp + http_msg.http_header]
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ecx, [ebp + http_msg.header_length]
|
|
|
|
.need_newline:
|
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jz .convert_done
|
|
|
|
cmp byte[esi], 10
|
|
|
|
jne .need_newline
|
2014-03-23 23:37:44 +04:00
|
|
|
; We have found a newline
|
|
|
|
; A line beginning with space or tabs has no header fields.
|
2013-11-04 17:07:21 +04:00
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jz .convert_done
|
|
|
|
cmp byte[esi], ' '
|
|
|
|
je .need_newline
|
|
|
|
cmp byte[esi], 9 ; horizontal tab
|
|
|
|
je .need_newline
|
|
|
|
jmp .convert_loop
|
|
|
|
.next_char:
|
|
|
|
inc esi
|
|
|
|
dec ecx
|
|
|
|
jz .convert_done
|
|
|
|
.convert_loop:
|
|
|
|
cmp byte[esi], ':'
|
|
|
|
je .need_newline
|
|
|
|
cmp byte[esi], 'A'
|
|
|
|
jb .next_char
|
|
|
|
cmp byte[esi], 'Z'
|
|
|
|
ja .next_char
|
|
|
|
or byte[esi], 0x20 ; convert to lowercase
|
|
|
|
jmp .next_char
|
|
|
|
.convert_done:
|
|
|
|
mov byte[esi-1], 0
|
2014-01-28 01:15:30 +04:00
|
|
|
lea esi, [ebp + http_msg.http_header]
|
2013-11-04 17:07:21 +04:00
|
|
|
DEBUGF 1, "Header names converted to lowercase:\n%s\n", esi
|
|
|
|
|
|
|
|
; Check for content-length header field.
|
2013-11-13 23:21:57 +04:00
|
|
|
stdcall HTTP_find_header_field, ebp, str_cl
|
2013-11-04 17:07:21 +04:00
|
|
|
test eax, eax
|
|
|
|
jz .no_content
|
|
|
|
or [ebp + http_msg.flags], FLAG_CONTENT_LENGTH
|
|
|
|
|
|
|
|
xor edx, edx
|
|
|
|
.cl_loop:
|
|
|
|
movzx ebx, byte[eax]
|
|
|
|
inc eax
|
|
|
|
cmp bl, 10
|
|
|
|
je .cl_ok
|
|
|
|
cmp bl, 13
|
|
|
|
je .cl_ok
|
|
|
|
cmp bl, ' '
|
|
|
|
je .cl_ok
|
|
|
|
sub bl, '0'
|
2014-03-23 23:37:44 +04:00
|
|
|
jb .err_header
|
2013-11-04 17:07:21 +04:00
|
|
|
cmp bl, 9
|
2014-03-23 23:37:44 +04:00
|
|
|
ja .err_header
|
2013-11-04 17:07:21 +04:00
|
|
|
lea edx, [edx + edx*4] ; edx = edx*10
|
|
|
|
shl edx, 1 ;
|
|
|
|
add edx, ebx
|
|
|
|
jmp .cl_loop
|
|
|
|
|
|
|
|
.cl_ok:
|
|
|
|
mov [ebp + http_msg.content_length], edx
|
|
|
|
DEBUGF 1, "Content-length: %u\n", edx
|
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
test edx, edx
|
|
|
|
jz .got_all_data
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
call alloc_contentbuff
|
|
|
|
test eax, eax
|
2014-03-23 23:37:44 +04:00
|
|
|
jz .err_no_ram
|
2014-01-28 01:15:30 +04:00
|
|
|
xor eax, eax
|
|
|
|
jmp .header_parsed
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
.no_content:
|
|
|
|
DEBUGF 1, "Content-length not found.\n"
|
|
|
|
; We didnt find 'content-length', maybe server is using chunked transfer encoding?
|
2015-03-20 17:00:21 +03:00
|
|
|
.multibuffer:
|
2013-11-04 17:07:21 +04:00
|
|
|
; Try to find 'transfer-encoding' header.
|
2013-11-13 23:21:57 +04:00
|
|
|
stdcall HTTP_find_header_field, ebp, str_te
|
2013-11-04 17:07:21 +04:00
|
|
|
test eax, eax
|
2014-01-28 01:15:30 +04:00
|
|
|
jnz .ct_hdr_found
|
|
|
|
|
|
|
|
.not_chunked:
|
|
|
|
mov edx, BUFFERSIZE
|
|
|
|
call alloc_contentbuff
|
|
|
|
test eax, eax
|
2014-03-23 23:37:44 +04:00
|
|
|
jz .err_no_ram
|
2014-01-28 01:15:30 +04:00
|
|
|
xor eax, eax
|
|
|
|
jmp .header_parsed
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
.ct_hdr_found:
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ebx, dword[eax]
|
2013-11-04 22:41:15 +04:00
|
|
|
or ebx, 0x20202020
|
2013-11-04 17:07:21 +04:00
|
|
|
cmp ebx, 'chun'
|
2013-11-13 00:08:02 +04:00
|
|
|
jne .not_chunked
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ebx, dword[eax+4]
|
2013-11-04 22:41:15 +04:00
|
|
|
or ebx, 0x00202020
|
|
|
|
and ebx, 0x00ffffff
|
2013-11-04 17:07:21 +04:00
|
|
|
cmp ebx, 'ked'
|
2013-11-13 00:08:02 +04:00
|
|
|
jne .not_chunked
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
or [ebp + http_msg.flags], FLAG_CHUNKED
|
|
|
|
DEBUGF 1, "Transfer type is: chunked\n"
|
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
mov edx, BUFFERSIZE
|
|
|
|
call alloc_contentbuff
|
|
|
|
test eax, eax
|
2014-03-23 23:37:44 +04:00
|
|
|
jz .err_no_ram
|
2014-01-28 01:15:30 +04:00
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
; Set chunk pointer where first chunk should begin.
|
2014-01-28 01:15:30 +04:00
|
|
|
mov eax, [ebp + http_msg.content_ptr]
|
2013-11-04 17:07:21 +04:00
|
|
|
mov [ebp + http_msg.chunk_ptr], eax
|
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
;--------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Chunk parsing code begins here
|
|
|
|
;
|
|
|
|
|
2013-11-04 22:41:15 +04:00
|
|
|
.chunk_loop:
|
2017-10-14 01:15:00 +03:00
|
|
|
DEBUGF 1, "chunk_loop write_ptr=0x%x chunk_ptr=0x%x\n", [ebp + http_msg.write_ptr], [ebp + http_msg.chunk_ptr]
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ecx, [ebp + http_msg.write_ptr]
|
|
|
|
sub ecx, [ebp + http_msg.chunk_ptr]
|
2017-10-14 01:15:00 +03:00
|
|
|
jbe .need_more_data_chunked ; amount of available bytes after chunkline start
|
2014-03-23 23:37:44 +04:00
|
|
|
|
2013-11-11 01:05:08 +04:00
|
|
|
; Chunkline starts here, convert the ASCII hex number into ebx
|
2013-11-04 17:07:21 +04:00
|
|
|
mov esi, [ebp + http_msg.chunk_ptr]
|
2014-03-23 23:37:44 +04:00
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
xor ebx, ebx
|
2014-03-23 23:37:44 +04:00
|
|
|
cmp byte[esi], 0x0d
|
|
|
|
jne .chunk_hex_loop
|
|
|
|
dec ecx
|
|
|
|
jz .need_more_data_chunked
|
|
|
|
inc esi
|
|
|
|
cmp byte[esi], 0x0a
|
|
|
|
jne .chunk_hex_loop
|
|
|
|
dec ecx
|
|
|
|
jz .need_more_data_chunked
|
|
|
|
inc esi
|
|
|
|
.chunk_hex_loop:
|
2013-11-04 17:07:21 +04:00
|
|
|
lodsb
|
|
|
|
sub al, '0'
|
2014-03-23 23:37:44 +04:00
|
|
|
jb .chunk_hex_end
|
2013-11-04 17:07:21 +04:00
|
|
|
cmp al, 9
|
|
|
|
jbe .chunk_hex
|
2013-11-04 22:41:15 +04:00
|
|
|
sub al, 'A' - '0' - 10
|
2014-03-23 23:37:44 +04:00
|
|
|
jb .chunk_hex_end
|
2013-11-04 22:41:15 +04:00
|
|
|
cmp al, 15
|
2013-11-04 17:07:21 +04:00
|
|
|
jbe .chunk_hex
|
|
|
|
sub al, 'a' - 'A'
|
2013-11-04 22:41:15 +04:00
|
|
|
cmp al, 15
|
2014-03-23 23:37:44 +04:00
|
|
|
ja .chunk_hex_end
|
2013-11-04 17:07:21 +04:00
|
|
|
.chunk_hex:
|
|
|
|
shl ebx, 4
|
|
|
|
add bl, al
|
2014-03-23 23:37:44 +04:00
|
|
|
dec ecx
|
|
|
|
jnz .chunk_hex_loop
|
|
|
|
jmp .need_more_data_chunked
|
|
|
|
.chunk_hex_end:
|
|
|
|
; Chunkline ends with a CR LF or simply LF
|
2014-01-28 01:15:30 +04:00
|
|
|
dec esi
|
2013-11-11 01:05:08 +04:00
|
|
|
.end_of_chunkline?:
|
2014-01-28 01:15:30 +04:00
|
|
|
lodsb
|
2014-03-23 23:37:44 +04:00
|
|
|
cmp al, 10 ; chunkline must always end with LF
|
2013-11-04 17:07:21 +04:00
|
|
|
je .end_of_chunkline
|
2014-03-23 23:37:44 +04:00
|
|
|
dec ecx
|
|
|
|
jnz .end_of_chunkline?
|
|
|
|
xor eax, eax
|
|
|
|
jmp .need_more_data_chunked ; chunkline is incomplete, request more data
|
2013-11-04 17:07:21 +04:00
|
|
|
.end_of_chunkline:
|
2014-03-23 23:37:44 +04:00
|
|
|
DEBUGF 1, "Chunk of 0x%x bytes\n", ebx
|
2014-01-28 01:15:30 +04:00
|
|
|
; If chunk size is 0, all chunks have been received.
|
|
|
|
test ebx, ebx
|
|
|
|
jz .got_all_data_chunked
|
2017-10-14 01:15:00 +03:00
|
|
|
; Calculate chunkline length
|
|
|
|
mov edx, esi
|
|
|
|
sub edx, [ebp + http_msg.chunk_ptr] ; edx is now length of chunkline
|
|
|
|
DEBUGF 1, "Chunkline is %u bytes long\n", edx
|
2014-03-23 23:37:44 +04:00
|
|
|
; Calculate how many data bytes we have received already
|
2014-01-28 01:15:30 +04:00
|
|
|
mov ecx, [ebp + http_msg.write_ptr]
|
2017-10-14 01:15:00 +03:00
|
|
|
sub ecx, [ebp + http_msg.chunk_ptr]
|
|
|
|
sub ecx, edx ; ecx is now number of received data bytes (without chunkline)
|
2014-03-23 23:37:44 +04:00
|
|
|
; Update content_received counter
|
|
|
|
add [ebp + http_msg.content_received], ecx
|
|
|
|
; Calculate new write ptr
|
|
|
|
sub [ebp + http_msg.write_ptr], edx
|
2015-03-20 17:00:21 +03:00
|
|
|
test [ebp + http_msg.flags], FLAG_STREAM
|
2015-03-20 15:04:14 +03:00
|
|
|
jnz .dont_resize
|
2013-11-04 22:41:15 +04:00
|
|
|
; Realloc buffer, make it 'chunksize' bigger.
|
2014-03-23 23:37:44 +04:00
|
|
|
lea edx, [ebx + BUFFERSIZE]
|
|
|
|
mov [ebp + http_msg.buffer_length], edx ; remaining space in new buffer
|
|
|
|
add edx, [ebp + http_msg.write_ptr]
|
|
|
|
sub edx, [ebp + http_msg.content_ptr]
|
|
|
|
DEBUGF 1, "Resizing buffer 0x%x, it will now be %u bytes\n", [ebp + http_msg.content_ptr], edx
|
|
|
|
invoke mem.realloc, [ebp + http_msg.content_ptr], edx
|
2014-01-28 01:15:30 +04:00
|
|
|
DEBUGF 1, "New buffer = 0x%x\n", eax
|
2013-11-04 22:41:15 +04:00
|
|
|
or eax, eax
|
2014-03-23 23:37:44 +04:00
|
|
|
jz .err_no_ram
|
|
|
|
call recalculate_pointers ; Because it's possible that buffer begins on another address now
|
|
|
|
add esi, eax ; recalculate esi too!
|
2015-03-20 15:04:14 +03:00
|
|
|
.dont_resize:
|
2014-03-23 23:37:44 +04:00
|
|
|
; Remove chunk header (aka chunkline) from the buffer by shifting all received data after chunkt_ptr to the left
|
2014-01-28 01:15:30 +04:00
|
|
|
mov edi, [ebp + http_msg.chunk_ptr]
|
2017-10-14 01:15:00 +03:00
|
|
|
DEBUGF 1, "Removing chunkline esi=0x%x edi=0x%x ecx=%u\n", esi, edi, ecx
|
2014-01-28 01:15:30 +04:00
|
|
|
rep movsb
|
2014-03-23 23:37:44 +04:00
|
|
|
; Update chunk ptr to point to next chunk
|
|
|
|
add [ebp + http_msg.chunk_ptr], ebx
|
|
|
|
; Set number of received bytes to 0, we already updated content_received
|
2014-01-28 01:15:30 +04:00
|
|
|
xor eax, eax
|
2013-11-04 22:41:15 +04:00
|
|
|
jmp .chunk_loop
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
;--------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; end of proc code begins here
|
|
|
|
;
|
|
|
|
|
2013-11-04 22:41:15 +04:00
|
|
|
.header_parsed:
|
2014-03-23 23:37:44 +04:00
|
|
|
; Header was already parsed and connection isnt chunked.
|
|
|
|
; Update content_received
|
2013-11-05 21:02:59 +04:00
|
|
|
add [ebp + http_msg.content_received], eax
|
2014-03-23 23:37:44 +04:00
|
|
|
; If we received content-length parameter, check if we received all the data
|
2013-11-13 00:08:02 +04:00
|
|
|
test [ebp + http_msg.flags], FLAG_CONTENT_LENGTH
|
2014-03-23 23:37:44 +04:00
|
|
|
jz @f
|
2013-11-12 14:59:36 +04:00
|
|
|
mov eax, [ebp + http_msg.content_received]
|
|
|
|
cmp eax, [ebp + http_msg.content_length]
|
2013-11-05 21:02:59 +04:00
|
|
|
jae .got_all_data
|
2014-03-23 23:37:44 +04:00
|
|
|
@@:
|
|
|
|
cmp [ebp + http_msg.buffer_length], 0
|
|
|
|
je .buffer_full
|
|
|
|
; Need more data
|
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
|
|
|
ret
|
2013-11-13 00:08:02 +04:00
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
.buffer_full:
|
2015-03-20 17:00:21 +03:00
|
|
|
test [ebp + http_msg.flags], FLAG_STREAM
|
2015-03-20 15:04:14 +03:00
|
|
|
jnz .multibuff
|
2013-11-13 00:08:02 +04:00
|
|
|
mov eax, [ebp + http_msg.write_ptr]
|
|
|
|
add eax, BUFFERSIZE
|
2014-01-28 01:15:30 +04:00
|
|
|
sub eax, [ebp + http_msg.content_ptr]
|
|
|
|
invoke mem.realloc, [ebp + http_msg.content_ptr], eax
|
2013-11-13 00:08:02 +04:00
|
|
|
or eax, eax
|
2014-03-23 23:37:44 +04:00
|
|
|
jz .err_no_ram
|
2014-01-28 01:15:30 +04:00
|
|
|
call recalculate_pointers
|
2013-11-13 00:08:02 +04:00
|
|
|
mov [ebp + http_msg.buffer_length], BUFFERSIZE
|
2014-03-23 23:37:44 +04:00
|
|
|
; Need more data
|
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
|
|
|
ret
|
2013-11-12 01:25:11 +04:00
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
.multibuff:
|
|
|
|
; This buffer is full
|
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
.need_more_data_for_header:
|
|
|
|
cmp [ebp + http_msg.buffer_length], 0
|
|
|
|
je .err_header ; It's just too damn long!
|
|
|
|
; Need more data
|
2013-11-04 22:41:15 +04:00
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
|
|
|
ret
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-04 22:41:15 +04:00
|
|
|
.need_more_data_chunked:
|
2014-03-23 23:37:44 +04:00
|
|
|
; We only got a partial chunk, or need more chunks, update content_received and request more data
|
2013-11-05 21:02:59 +04:00
|
|
|
add [ebp + http_msg.content_received], eax
|
2013-11-04 17:07:21 +04:00
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
|
|
|
ret
|
|
|
|
|
2013-11-04 22:41:15 +04:00
|
|
|
.got_all_data_chunked:
|
2014-03-23 23:37:44 +04:00
|
|
|
; Woohoo, we got all the chunked data, calculate total number of bytes received.
|
2013-11-04 22:41:15 +04:00
|
|
|
mov eax, [ebp + http_msg.chunk_ptr]
|
2014-01-28 01:15:30 +04:00
|
|
|
sub eax, [ebp + http_msg.content_ptr]
|
2013-11-04 22:41:15 +04:00
|
|
|
mov [ebp + http_msg.content_length], eax
|
2013-11-05 21:02:59 +04:00
|
|
|
mov [ebp + http_msg.content_received], eax
|
2013-11-04 17:07:21 +04:00
|
|
|
.got_all_data:
|
2013-11-12 14:59:36 +04:00
|
|
|
DEBUGF 1, "We got all the data! (%u bytes)\n", [ebp + http_msg.content_received]
|
2013-11-11 15:35:18 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_GOT_ALL_DATA
|
2015-08-24 16:43:34 +03:00
|
|
|
test [ebp + http_msg.flags], FLAG_KEEPALIVE
|
|
|
|
jnz @f
|
2013-11-04 17:07:21 +04:00
|
|
|
mcall close, [ebp + http_msg.socket]
|
2015-08-24 16:43:34 +03:00
|
|
|
and [ebp + http_msg.flags], not FLAG_CONNECTED
|
|
|
|
@@:
|
2013-11-04 17:07:21 +04:00
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
;--------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; error handeling code begins here
|
|
|
|
;
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
.check_socket:
|
|
|
|
cmp ebx, EWOULDBLOCK
|
2014-03-23 23:37:44 +04:00
|
|
|
jne .err_socket
|
|
|
|
mcall 26, 9
|
|
|
|
sub eax, [ebp + http_msg.timestamp]
|
|
|
|
cmp eax, TIMEOUT
|
|
|
|
ja .err_timeout
|
|
|
|
; Need more data
|
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
|
|
|
ret
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-13 00:08:02 +04:00
|
|
|
.server_closed:
|
|
|
|
DEBUGF 1, "server closed connection, transfer complete?\n"
|
|
|
|
test [ebp + http_msg.flags], FLAG_GOT_HEADER
|
2014-03-23 23:37:44 +04:00
|
|
|
jz .err_server_closed
|
2013-11-13 00:08:02 +04:00
|
|
|
test [ebp + http_msg.flags], FLAG_CONTENT_LENGTH
|
|
|
|
jz .got_all_data
|
2014-03-23 23:37:44 +04:00
|
|
|
.err_server_closed:
|
2013-11-13 00:08:02 +04:00
|
|
|
pop eax
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: server closed connection unexpectedly\n"
|
2013-11-13 00:08:02 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_TRANSFER_FAILED
|
2014-03-23 23:37:44 +04:00
|
|
|
jmp .abort
|
2013-11-13 00:08:02 +04:00
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
.err_header:
|
2013-11-11 02:05:06 +04:00
|
|
|
pop eax
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: invalid header\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_INVALID_HEADER
|
2014-03-23 23:37:44 +04:00
|
|
|
jmp .abort
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
.err_no_ram:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: out of RAM\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_NO_RAM
|
2014-03-23 23:37:44 +04:00
|
|
|
jmp .abort
|
|
|
|
|
|
|
|
.err_timeout:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: timeout\n"
|
2014-03-23 23:37:44 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_TIMEOUT_ERROR
|
|
|
|
jmp .abort
|
2013-11-11 16:29:54 +04:00
|
|
|
|
2014-03-23 23:37:44 +04:00
|
|
|
.err_socket:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: socket error %u\n", ebx
|
2013-11-11 16:29:54 +04:00
|
|
|
or [ebp + http_msg.flags], FLAG_SOCKET_ERROR
|
2014-03-23 23:37:44 +04:00
|
|
|
.abort:
|
2013-11-11 15:35:18 +04:00
|
|
|
and [ebp + http_msg.flags], not FLAG_CONNECTED
|
|
|
|
mcall close, [ebp + http_msg.socket]
|
2013-11-11 16:29:54 +04:00
|
|
|
.connection_closed:
|
2013-11-04 17:07:21 +04:00
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
alloc_contentbuff:
|
|
|
|
|
2015-03-20 17:00:21 +03:00
|
|
|
test [ebp + http_msg.flags], FLAG_STREAM
|
|
|
|
jz @f
|
|
|
|
mov edx, BUFFERSIZE
|
|
|
|
@@:
|
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
; Allocate content buffer
|
|
|
|
invoke mem.alloc, edx
|
|
|
|
or eax, eax
|
|
|
|
jz .no_ram
|
|
|
|
|
|
|
|
DEBUGF 1, "Content buffer allocated: 0x%x\n", eax
|
|
|
|
|
|
|
|
; Copy already received content into content buffer
|
|
|
|
mov edi, eax
|
|
|
|
lea esi, [ebp + http_msg.http_header]
|
|
|
|
add esi, [ebp + http_msg.header_length]
|
|
|
|
mov ecx, [ebp + http_msg.write_ptr]
|
|
|
|
sub ecx, esi
|
|
|
|
mov ebx, ecx
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
; Update pointers to point to new buffer
|
|
|
|
mov [ebp + http_msg.content_ptr], eax
|
|
|
|
mov [ebp + http_msg.content_received], ebx
|
|
|
|
sub edx, ebx
|
|
|
|
mov [ebp + http_msg.buffer_length], edx
|
|
|
|
add eax, ebx
|
|
|
|
mov [ebp + http_msg.write_ptr], eax
|
|
|
|
|
|
|
|
; Shrink header buffer
|
|
|
|
mov eax, http_msg.http_header
|
|
|
|
add eax, [ebp + http_msg.header_length]
|
|
|
|
invoke mem.realloc, ebp, eax
|
|
|
|
or eax, eax
|
|
|
|
.no_ram:
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
recalculate_pointers:
|
|
|
|
|
|
|
|
sub eax, [ebp + http_msg.content_ptr]
|
|
|
|
jz .done
|
|
|
|
add [ebp + http_msg.content_ptr], eax
|
|
|
|
add [ebp + http_msg.write_ptr], eax
|
|
|
|
add [ebp + http_msg.chunk_ptr], eax
|
|
|
|
|
|
|
|
.done:
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-11 15:35:18 +04:00
|
|
|
;;================================================================================================;;
|
2014-07-16 18:11:19 +04:00
|
|
|
proc HTTP_send identifier, dataptr, datalength ;//////////////////////////////////////////////////;;
|
2013-11-11 15:35:18 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2014-07-16 18:11:19 +04:00
|
|
|
;? Send data to the server ;;
|
2013-11-11 15:35:18 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> identifier = pointer to buffer containing http_msg struct. ;;
|
2014-07-16 18:11:19 +04:00
|
|
|
;> dataptr = pointer to data to be sent. ;;
|
|
|
|
;> datalength = length of data (in bytes) to be sent ;;
|
2013-11-11 15:35:18 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2014-07-16 18:11:19 +04:00
|
|
|
;< eax = number of bytes sent, -1 on error ;;
|
2013-11-11 15:35:18 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
|
2014-07-16 18:11:19 +04:00
|
|
|
push ebx ecx edx esi edi
|
|
|
|
mov edx, [identifier]
|
|
|
|
test [edx + http_msg.flags], FLAG_CONNECTED
|
|
|
|
jz .fail
|
|
|
|
mcall send, [edx + http_msg.socket], [dataptr], [datalength], 0
|
|
|
|
pop edi esi edx ecx ebx
|
2013-11-11 15:35:18 +04:00
|
|
|
ret
|
|
|
|
|
2014-07-16 18:11:19 +04:00
|
|
|
.fail:
|
|
|
|
pop edi esi edx ecx ebx
|
|
|
|
xor eax, eax
|
|
|
|
dec eax
|
2013-11-11 15:35:18 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
;;================================================================================================;;
|
2013-11-13 23:21:57 +04:00
|
|
|
proc HTTP_find_header_field identifier, headername ;//////////////////////////////////////////////;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;? Find a header field in the received HTTP header ;;
|
2014-07-16 18:11:19 +04:00
|
|
|
;? ;;
|
|
|
|
;? NOTE: this function returns a pointer which points into the original header data. ;;
|
|
|
|
;? The header field is terminated by a CR, LF, space or maybe even tab. ;;
|
|
|
|
;? A free operation should not be operated on this pointer! ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;> identifier = ptr to http_msg struct ;;
|
2014-07-16 18:11:19 +04:00
|
|
|
;> headername = ptr to ASCIIZ string containing field you want to find (must be in lowercase) ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;< eax = 0 (error) / ptr to content of the HTTP header field ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
push ebx ecx edx esi edi
|
|
|
|
|
|
|
|
DEBUGF 1, "Find header field: %s\n", [headername]
|
|
|
|
|
|
|
|
mov ebx, [identifier]
|
2013-11-11 01:05:08 +04:00
|
|
|
test [ebx + http_msg.flags], FLAG_GOT_HEADER
|
|
|
|
jz .fail
|
|
|
|
|
2014-01-28 01:15:30 +04:00
|
|
|
lea edx, [ebx + http_msg.http_header]
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ecx, edx
|
|
|
|
add ecx, [ebx + http_msg.header_length]
|
|
|
|
|
|
|
|
.restart:
|
|
|
|
mov esi, [headername]
|
|
|
|
mov edi, edx
|
|
|
|
.loop:
|
|
|
|
cmp edi, ecx
|
|
|
|
jae .fail
|
|
|
|
lodsb
|
|
|
|
scasb
|
|
|
|
je .loop
|
|
|
|
test al, al
|
|
|
|
jz .done?
|
|
|
|
.next:
|
|
|
|
inc edx
|
|
|
|
jmp .restart
|
|
|
|
|
|
|
|
.not_done:
|
|
|
|
inc edi
|
|
|
|
.done?:
|
|
|
|
cmp byte[edi-1], ':'
|
|
|
|
je .almost_done
|
|
|
|
cmp byte[edi-1], ' '
|
|
|
|
je .not_done
|
|
|
|
cmp byte[edi-1], 9 ; tab
|
|
|
|
je .not_done
|
|
|
|
|
|
|
|
jmp .next
|
|
|
|
|
|
|
|
.almost_done: ; FIXME: buffer overflow?
|
|
|
|
dec edi
|
|
|
|
DEBUGF 1, "Found header field\n"
|
|
|
|
.spaceloop:
|
|
|
|
inc edi
|
|
|
|
cmp byte[edi], ' '
|
|
|
|
je .spaceloop
|
|
|
|
cmp byte[edi], 9 ; tab
|
|
|
|
je .spaceloop
|
|
|
|
|
|
|
|
mov eax, edi
|
|
|
|
pop edi esi edx ecx ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
2017-09-24 00:27:30 +03:00
|
|
|
DEBUGF 1, "Header field not found\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
pop edi esi edx ecx ebx
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2013-11-11 01:05:08 +04:00
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
;;================================================================================================;;
|
2015-03-20 15:04:14 +03:00
|
|
|
proc HTTP_escape URI, length ;////////////////////////////////////////////////////////////////////;;
|
2013-11-11 21:28:49 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2015-03-20 15:04:14 +03:00
|
|
|
;> URI = ptr to ASCIIZ URI/data ;;
|
|
|
|
;> length = length of URI/data ;;
|
2013-11-11 21:28:49 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2014-04-14 18:39:53 +04:00
|
|
|
;< eax = 0 (error) / ptr to ASCIIZ URI/data ;;
|
|
|
|
;< ebx = length of escaped URI/data ;;
|
2013-11-11 21:28:49 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
|
2014-04-17 23:20:35 +04:00
|
|
|
DEBUGF 1, "HTTP_escape: %s\n", [URI]
|
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
pusha
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
invoke mem.alloc, URLMAXLEN ; FIXME: use length provided by caller to guess final size.
|
2013-11-11 21:28:49 +04:00
|
|
|
test eax, eax
|
|
|
|
jz .error
|
2017-10-18 00:18:36 +03:00
|
|
|
mov edx, URLMAXLEN-1 ; Remaining space in temp buffer minus one for 0 byte
|
2013-11-11 21:28:49 +04:00
|
|
|
mov [esp + 7 * 4], eax ; return ptr in eax
|
|
|
|
mov esi, [URI]
|
|
|
|
mov edi, eax
|
|
|
|
xor ebx, ebx
|
|
|
|
xor ecx, ecx
|
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .done
|
|
|
|
|
|
|
|
mov cl, al
|
|
|
|
and cl, 0x1f
|
|
|
|
mov bl, al
|
2014-04-14 18:39:53 +04:00
|
|
|
shr bl, 3
|
|
|
|
and bl, not 3
|
2013-11-11 21:28:49 +04:00
|
|
|
bt dword[bits_must_escape + ebx], ecx
|
|
|
|
jc .escape
|
|
|
|
|
|
|
|
stosb
|
2017-10-18 00:18:36 +03:00
|
|
|
dec edx
|
|
|
|
jnz .loop
|
|
|
|
jmp .out_of_space
|
2013-11-11 21:28:49 +04:00
|
|
|
|
|
|
|
.escape:
|
2017-10-18 00:18:36 +03:00
|
|
|
sub edx, 3
|
|
|
|
jbe .out_of_space
|
2013-11-11 21:28:49 +04:00
|
|
|
mov al, '%'
|
|
|
|
stosb
|
|
|
|
mov bl, byte[esi-1]
|
|
|
|
shr bl, 4
|
|
|
|
mov al, byte[str_hex + ebx]
|
|
|
|
stosb
|
|
|
|
mov bl, byte[esi-1]
|
|
|
|
and bl, 0x0f
|
|
|
|
mov al, byte[str_hex + ebx]
|
|
|
|
stosb
|
|
|
|
jmp .loop
|
|
|
|
|
|
|
|
|
2017-10-18 00:18:36 +03:00
|
|
|
.out_of_space:
|
|
|
|
DEBUGF 2, "ERROR: buffer too small!\n"
|
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
.done:
|
2017-10-18 00:18:36 +03:00
|
|
|
xor al, al
|
2013-11-11 21:28:49 +04:00
|
|
|
stosb
|
2014-04-14 18:39:53 +04:00
|
|
|
sub edi, [esp + 7 * 4]
|
|
|
|
dec edi
|
|
|
|
mov [esp + 4 * 4], edi
|
2013-11-11 21:28:49 +04:00
|
|
|
|
|
|
|
popa
|
2014-04-17 23:20:35 +04:00
|
|
|
DEBUGF 1, "escaped URL: %s\n", eax
|
2013-11-11 21:28:49 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: out of RAM!\n"
|
2013-11-11 21:28:49 +04:00
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;================================================================================================;;
|
2015-03-20 15:04:14 +03:00
|
|
|
proc HTTP_unescape URI, length ;//////////////////////////////////////////////////////////////////;;
|
2013-11-11 21:28:49 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> URI = ptr to ASCIIZ URI ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< eax = 0 (error) / ptr to ASCIIZ URI ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
|
2014-04-17 23:20:35 +04:00
|
|
|
DEBUGF 1, "HTTP_unescape: %s\n", [URI]
|
2013-11-11 21:28:49 +04:00
|
|
|
pusha
|
|
|
|
|
2015-03-20 15:04:14 +03:00
|
|
|
invoke mem.alloc, URLMAXLEN ; FIXME: use length provided by caller
|
2013-11-11 21:28:49 +04:00
|
|
|
test eax, eax
|
|
|
|
jz .error
|
2017-10-18 00:18:36 +03:00
|
|
|
mov edx, URLMAXLEN-1 ; Remaining space in temp buffer minus one for 0 byte
|
2013-11-11 21:28:49 +04:00
|
|
|
mov [esp + 7 * 4], eax ; return ptr in eax
|
|
|
|
mov esi, [URI]
|
|
|
|
mov edi, eax
|
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .done
|
|
|
|
cmp al, '%'
|
|
|
|
je .unescape
|
|
|
|
stosb
|
2017-10-18 00:18:36 +03:00
|
|
|
dec edx
|
|
|
|
jnz .loop
|
|
|
|
jmp .out_of_space
|
2013-11-11 21:28:49 +04:00
|
|
|
|
|
|
|
.unescape:
|
|
|
|
xor ebx, ebx
|
|
|
|
xor ecx, ecx
|
|
|
|
.unescape_nibble:
|
|
|
|
lodsb
|
|
|
|
sub al, '0'
|
|
|
|
jb .fail
|
|
|
|
cmp al, 9
|
|
|
|
jbe .nibble_ok
|
|
|
|
sub al, 'A' - '0' - 10
|
|
|
|
jb .fail
|
|
|
|
cmp al, 15
|
|
|
|
jbe .nibble_ok
|
|
|
|
sub al, 'a' - 'A'
|
|
|
|
cmp al, 15
|
|
|
|
ja .fail
|
|
|
|
.nibble_ok:
|
|
|
|
shl bl, 8
|
|
|
|
or bl, al
|
|
|
|
dec ecx
|
|
|
|
jc .unescape_nibble
|
|
|
|
mov al, bl
|
|
|
|
stosb
|
2017-10-18 00:18:36 +03:00
|
|
|
dec edx
|
|
|
|
jnz .loop
|
|
|
|
jmp .out_of_space
|
2013-11-11 21:28:49 +04:00
|
|
|
|
|
|
|
.fail:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: invalid URI!\n"
|
2013-11-11 21:28:49 +04:00
|
|
|
jmp .loop
|
|
|
|
|
2017-10-18 00:18:36 +03:00
|
|
|
.out_of_space:
|
|
|
|
DEBUGF 2, "ERROR: buffer too small!\n"
|
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
.done:
|
2017-10-18 00:18:36 +03:00
|
|
|
xor al, al
|
2013-11-11 21:28:49 +04:00
|
|
|
stosb
|
|
|
|
popa
|
2014-04-17 23:20:35 +04:00
|
|
|
DEBUGF 1, "unescaped URL: %s\n", eax
|
2013-11-11 21:28:49 +04:00
|
|
|
ret
|
|
|
|
|
|
|
|
.error:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "ERROR: out of RAM!\n"
|
2013-11-11 21:28:49 +04:00
|
|
|
popa
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-11 01:05:08 +04:00
|
|
|
|
2013-11-11 15:35:18 +04:00
|
|
|
|
2013-11-11 01:05:08 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
;! Internal procedures section ;;
|
2013-11-13 00:08:02 +04:00
|
|
|
;; ;;
|
|
|
|
;; NOTICE: These procedures do not follow stdcall conventions and thus may destroy any register. ;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
proc open_connection hostname, port ;/////////////////////////////////////////////////////////////;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;? Connects to a HTTP server ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;> hostname = ptr to ASCIIZ hostname ;;
|
|
|
|
;> port = port (x86 byte order) ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;< eax = 0 (error) / socketnum ;;
|
2013-11-05 15:22:31 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
|
|
|
|
locals
|
|
|
|
sockaddr dd ?
|
|
|
|
socketnum dd ?
|
|
|
|
endl
|
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
cmp [proxyAddr], 0
|
|
|
|
je .no_proxy
|
|
|
|
|
|
|
|
mov [hostname], proxyAddr
|
|
|
|
|
|
|
|
push [proxyPort]
|
|
|
|
pop [port]
|
|
|
|
.no_proxy:
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
; Resolve the hostname
|
|
|
|
DEBUGF 1, "Resolving hostname\n"
|
|
|
|
push esp ; reserve stack place
|
2017-06-11 13:50:58 +03:00
|
|
|
invoke getaddrinfo, [hostname], 0, 0, esp
|
2013-11-05 15:22:31 +04:00
|
|
|
pop esi
|
|
|
|
test eax, eax
|
|
|
|
jnz .error1
|
|
|
|
|
|
|
|
; getaddrinfo returns addrinfo struct, make the pointer to sockaddr struct
|
2015-11-10 00:47:39 +03:00
|
|
|
push esi ; for freeaddrinfo
|
2013-11-05 15:22:31 +04:00
|
|
|
mov esi, [esi + addrinfo.ai_addr]
|
|
|
|
mov [sockaddr], esi
|
|
|
|
mov eax, [esi + sockaddr_in.sin_addr]
|
|
|
|
test eax, eax
|
|
|
|
jz .error2
|
|
|
|
|
|
|
|
DEBUGF 1, "Server ip=%u.%u.%u.%u\n", \
|
|
|
|
[esi + sockaddr_in.sin_addr]:1, [esi + sockaddr_in.sin_addr + 1]:1, \
|
|
|
|
[esi + sockaddr_in.sin_addr + 2]:1, [esi + sockaddr_in.sin_addr + 3]:1
|
|
|
|
|
|
|
|
mov [esi + sockaddr_in.sin_family], AF_INET4
|
|
|
|
mov eax, [port]
|
|
|
|
xchg al, ah
|
|
|
|
mov [esi + sockaddr_in.sin_port], ax
|
|
|
|
|
2017-06-11 13:50:58 +03:00
|
|
|
; Open a new TCP socket
|
2013-11-05 15:22:31 +04:00
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
|
|
|
test eax, eax
|
2017-06-11 13:50:58 +03:00
|
|
|
jz .error3
|
2013-11-05 15:22:31 +04:00
|
|
|
mov [socketnum], eax
|
|
|
|
DEBUGF 1, "Socket: 0x%x\n", eax
|
|
|
|
|
2017-06-11 13:50:58 +03:00
|
|
|
; Connect to the server
|
2013-11-05 15:22:31 +04:00
|
|
|
mcall connect, [socketnum], [sockaddr], 18
|
|
|
|
test eax, eax
|
2017-06-11 13:50:58 +03:00
|
|
|
jnz .error3
|
2013-11-05 15:22:31 +04:00
|
|
|
DEBUGF 1, "Socket is now connected.\n"
|
|
|
|
|
2017-06-11 13:50:58 +03:00
|
|
|
invoke freeaddrinfo ; Free allocated memory
|
2013-11-05 15:22:31 +04:00
|
|
|
mov eax, [socketnum]
|
|
|
|
ret
|
|
|
|
|
2017-06-11 13:50:58 +03:00
|
|
|
.error3:
|
|
|
|
DEBUGF 2, "Could not connect to the remote server\n"
|
|
|
|
invoke freeaddrinfo ; Free allocated memory
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
.error2:
|
2017-06-11 13:50:58 +03:00
|
|
|
DEBUGF 2, "Resolving hostname failed\n"
|
|
|
|
invoke freeaddrinfo ; Free allocated memory
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
.error1:
|
2017-09-25 00:26:43 +03:00
|
|
|
DEBUGF 2, "Contacting DNS server failed with EAI code: %x\n", eax
|
2013-11-05 15:22:31 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
;;================================================================================================;;
|
|
|
|
proc parse_url URL ;//////////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;? Split a given URL into hostname and pageaddr ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;> URL = ptr to ASCIIZ URL ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;< eax = 0 (error) / ptr to ASCIIZ hostname ;;
|
|
|
|
;< ebx = ptr to ASCIIZ pageaddr ;;
|
2013-11-15 11:49:31 +04:00
|
|
|
;< ecx = port number ;;
|
2013-11-04 17:07:21 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
|
|
|
|
locals
|
|
|
|
urlsize dd ?
|
|
|
|
hostname dd ?
|
|
|
|
pageaddr dd ?
|
2013-11-15 11:49:31 +04:00
|
|
|
port dd ?
|
2013-11-04 17:07:21 +04:00
|
|
|
endl
|
|
|
|
|
2013-11-04 20:14:13 +04:00
|
|
|
DEBUGF 1, "parsing URL: %s\n", [URL]
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
; remove any leading protocol text
|
2014-06-18 15:31:21 +04:00
|
|
|
mov edi, [URL]
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ecx, URLMAXLEN
|
|
|
|
mov ax, '//'
|
|
|
|
.loop1:
|
2014-06-18 15:31:21 +04:00
|
|
|
cmp byte[edi], 0 ; end of URL?
|
2013-11-04 17:07:21 +04:00
|
|
|
je .url_ok ; yep, so not found
|
2014-06-18 15:31:21 +04:00
|
|
|
cmp [edi], ax
|
2013-11-04 17:07:21 +04:00
|
|
|
je .skip_proto
|
2014-06-18 15:31:21 +04:00
|
|
|
inc edi
|
2013-11-04 17:07:21 +04:00
|
|
|
dec ecx
|
|
|
|
jnz .loop1
|
2013-11-15 11:49:31 +04:00
|
|
|
jmp .invalid
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
.skip_proto:
|
2014-06-18 15:31:21 +04:00
|
|
|
inc edi ; skip the two '/'
|
|
|
|
inc edi
|
|
|
|
mov [URL], edi ; update pointer so it skips protocol
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2014-06-18 15:31:21 +04:00
|
|
|
; Find the trailing 0 byte
|
|
|
|
xor al, al
|
|
|
|
repne scasb
|
|
|
|
jne .invalid ; ecx reached 0 before we reached end of string
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2014-06-18 15:31:21 +04:00
|
|
|
.url_ok:
|
|
|
|
sub edi, [URL] ; calculate total length of URL
|
|
|
|
mov [urlsize], edi
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
; now look for page delimiter - it's a '/' character
|
2014-06-18 15:31:21 +04:00
|
|
|
mov ecx, edi ; URL length
|
2013-11-04 17:07:21 +04:00
|
|
|
mov edi, [URL]
|
|
|
|
mov al, '/'
|
|
|
|
repne scasb
|
2013-11-04 20:14:13 +04:00
|
|
|
jne @f
|
2013-11-04 17:07:21 +04:00
|
|
|
dec edi ; return one char, '/' must be part of the pageaddr
|
|
|
|
inc ecx ;
|
2013-11-04 20:14:13 +04:00
|
|
|
@@:
|
2013-11-04 17:07:21 +04:00
|
|
|
push ecx edi ; remember the pointer and length of pageaddr
|
|
|
|
|
2013-11-13 23:21:57 +04:00
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
; Create new buffer and put hostname in it.
|
2013-11-04 17:07:21 +04:00
|
|
|
mov ecx, edi
|
|
|
|
sub ecx, [URL]
|
|
|
|
inc ecx ; we will add a 0 byte at the end
|
|
|
|
invoke mem.alloc, ecx
|
|
|
|
or eax, eax
|
|
|
|
jz .no_mem
|
|
|
|
|
|
|
|
mov [hostname], eax ; copy hostname to buffer
|
|
|
|
mov edi, eax
|
|
|
|
mov esi, [URL]
|
|
|
|
dec ecx
|
|
|
|
rep movsb
|
|
|
|
xor al, al
|
|
|
|
stosb
|
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
; Check if user provided a port, and convert it if so.
|
|
|
|
mov esi, [hostname]
|
|
|
|
mov [port], 80 ; default port if user didnt provide one
|
|
|
|
.portloop:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .no_port
|
|
|
|
cmp al, ':'
|
|
|
|
jne .portloop
|
|
|
|
|
|
|
|
push esi
|
|
|
|
call ascii_dec_ebx
|
|
|
|
pop edi
|
|
|
|
cmp byte[esi-1], 0
|
|
|
|
jne .invalid
|
|
|
|
cmp [proxyAddr], 0 ; remove port number from hostname
|
|
|
|
jne @f ; unless when we are using proxy
|
|
|
|
mov byte[edi-1], 0
|
|
|
|
@@:
|
|
|
|
test ebx, ebx
|
|
|
|
je .invalid
|
|
|
|
cmp ebx, 0xffff
|
|
|
|
ja .invalid
|
|
|
|
mov [port], ebx
|
|
|
|
.no_port:
|
|
|
|
|
|
|
|
|
|
|
|
; Did user provide a pageaddr?
|
2013-11-04 20:14:13 +04:00
|
|
|
mov [pageaddr], str_slash ; assume there is no pageaddr
|
2013-11-04 17:07:21 +04:00
|
|
|
pop esi ecx
|
|
|
|
test ecx, ecx
|
|
|
|
jz .no_page
|
2013-11-15 11:49:31 +04:00
|
|
|
|
|
|
|
; Create new buffer and put pageaddr into it.
|
2013-11-04 17:07:21 +04:00
|
|
|
inc ecx ; we will add a 0 byte at the end
|
|
|
|
invoke mem.alloc, ecx
|
|
|
|
or eax, eax
|
|
|
|
jz .no_mem
|
|
|
|
|
|
|
|
mov [pageaddr], eax ; copy pageaddr to buffer
|
|
|
|
mov edi, eax
|
|
|
|
dec ecx
|
|
|
|
rep movsb
|
|
|
|
xor al, al
|
|
|
|
stosb
|
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
.no_page:
|
2013-11-04 17:07:21 +04:00
|
|
|
mov eax, [hostname]
|
|
|
|
mov ebx, [pageaddr]
|
2013-11-15 11:49:31 +04:00
|
|
|
mov ecx, [port]
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
DEBUGF 1, "hostname: %s\n", eax
|
|
|
|
DEBUGF 1, "pageaddr: %s\n", ebx
|
2013-11-13 22:25:47 +04:00
|
|
|
DEBUGF 1, "port: %u\n", ecx
|
2013-11-04 17:07:21 +04:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.no_mem:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "Out of memory!\n"
|
2013-11-15 11:49:31 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.invalid:
|
2015-08-17 13:33:23 +03:00
|
|
|
DEBUGF 2, "Invalid URL!\n"
|
2013-11-04 17:07:21 +04:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-11 01:05:08 +04:00
|
|
|
;;================================================================================================;;
|
2013-11-15 11:49:31 +04:00
|
|
|
proc append_proxy_auth_header ;///////////////////////////////////////////////////////////////////;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? Appends the proxy authentication header ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> / ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< / ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
mov esi, str_proxy_auth
|
|
|
|
mov ecx, str_proxy_auth.length
|
|
|
|
rep movsb
|
|
|
|
; base64-encode string <user>:<password>
|
|
|
|
mov esi, proxyUser
|
|
|
|
|
|
|
|
apah000:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz apah001
|
|
|
|
call encode_base64_byte
|
|
|
|
jmp apah000
|
|
|
|
|
|
|
|
apah001:
|
|
|
|
mov al, ':'
|
|
|
|
call encode_base64_byte
|
|
|
|
mov esi, proxyPassword
|
|
|
|
|
|
|
|
apah002:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz apah003
|
|
|
|
call encode_base64_byte
|
|
|
|
jmp apah002
|
|
|
|
|
|
|
|
apah003:
|
|
|
|
call encode_base64_final
|
|
|
|
ret
|
|
|
|
|
|
|
|
encode_base64_byte:
|
|
|
|
inc ecx
|
|
|
|
shl edx, 8
|
|
|
|
mov dl, al
|
|
|
|
cmp ecx, 3
|
|
|
|
je ebb001
|
|
|
|
ret
|
|
|
|
|
|
|
|
ebb001:
|
|
|
|
shl edx, 8
|
|
|
|
inc ecx
|
|
|
|
|
|
|
|
ebb002:
|
|
|
|
rol edx, 6
|
|
|
|
xor eax, eax
|
|
|
|
xchg al, dl
|
|
|
|
mov al, [base64_table+eax]
|
|
|
|
stosb
|
|
|
|
loop ebb002
|
|
|
|
ret
|
|
|
|
|
|
|
|
encode_base64_final:
|
|
|
|
mov al, 0
|
|
|
|
test ecx, ecx
|
|
|
|
jz ebf000
|
|
|
|
call encode_base64_byte
|
|
|
|
test ecx, ecx
|
|
|
|
jz ebf001
|
|
|
|
call encode_base64_byte
|
|
|
|
mov byte [edi-2], '='
|
|
|
|
|
|
|
|
ebf001:
|
|
|
|
mov byte [edi-1], '='
|
|
|
|
|
|
|
|
ebf000:
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;;================================================================================================;;
|
|
|
|
proc eax_ascii_dec ;//////////////////////////////////////////////////////////////////////////////;;
|
2013-11-11 01:05:08 +04:00
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? Convert eax to ASCII decimal number ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> eax = number ;;
|
|
|
|
;> edi = ptr where to write ASCII decimal number ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;< / ;;
|
|
|
|
;;================================================================================================;;
|
2013-11-05 15:22:31 +04:00
|
|
|
|
2013-11-05 21:02:59 +04:00
|
|
|
push -'0'
|
2013-11-05 15:22:31 +04:00
|
|
|
mov ecx, 10
|
|
|
|
.loop:
|
|
|
|
xor edx, edx
|
|
|
|
div ecx
|
2013-11-05 21:02:59 +04:00
|
|
|
push edx
|
2013-11-05 15:22:31 +04:00
|
|
|
test eax, eax
|
|
|
|
jnz .loop
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-05 21:02:59 +04:00
|
|
|
.loop2:
|
|
|
|
pop eax
|
|
|
|
add al, '0'
|
|
|
|
jz .done
|
|
|
|
stosb
|
|
|
|
jmp .loop2
|
|
|
|
.done:
|
|
|
|
|
2013-11-05 15:22:31 +04:00
|
|
|
ret
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-11 01:05:08 +04:00
|
|
|
endp
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
proc ascii_dec_ebx ;//////////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;? Convert ASCII decimal number to ebx ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> esi = ptr where to read ASCII decimal number ;;
|
|
|
|
;;------------------------------------------------------------------------------------------------;;
|
|
|
|
;> ebx = number ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
xor ebx, ebx
|
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
sub al, '0'
|
|
|
|
jb .done
|
|
|
|
cmp al, 9
|
|
|
|
ja .done
|
|
|
|
lea ebx, [ebx + 4*ebx]
|
|
|
|
shl ebx, 1
|
|
|
|
add ebx, eax
|
|
|
|
jmp .loop
|
|
|
|
.done:
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
;;================================================================================================;;
|
|
|
|
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
;! Imported functions section ;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;================================================================================================;;
|
|
|
|
|
|
|
|
|
|
|
|
align 16
|
|
|
|
@IMPORT:
|
|
|
|
|
|
|
|
library \
|
|
|
|
libini, 'libini.obj', \
|
|
|
|
network, 'network.obj'
|
|
|
|
|
|
|
|
import libini, \
|
|
|
|
ini.get_str, 'ini_get_str', \
|
|
|
|
ini.get_int, 'ini_get_int'
|
|
|
|
|
|
|
|
import network,\
|
|
|
|
getaddrinfo, 'getaddrinfo',\
|
|
|
|
freeaddrinfo, 'freeaddrinfo',\
|
|
|
|
inet_ntoa, 'inet_ntoa'
|
|
|
|
|
|
|
|
;;===========================================================================;;
|
|
|
|
;;///////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
;! Exported functions section ;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
;;///////////////////////////////////////////////////////////////////////////;;
|
|
|
|
;;===========================================================================;;
|
|
|
|
|
|
|
|
|
2014-07-16 18:11:19 +04:00
|
|
|
HTTP_stop = HTTP_disconnect
|
|
|
|
HTTP_process = HTTP_receive
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
align 4
|
|
|
|
@EXPORT:
|
|
|
|
export \
|
|
|
|
lib_init , 'lib_init' , \
|
|
|
|
0x00010001 , 'version' , \
|
|
|
|
HTTP_get , 'get' , \
|
2013-11-05 15:22:31 +04:00
|
|
|
HTTP_head , 'head' , \
|
|
|
|
HTTP_post , 'post' , \
|
2013-11-13 23:21:57 +04:00
|
|
|
HTTP_find_header_field , 'find_header_field' , \
|
2014-07-16 18:11:19 +04:00
|
|
|
HTTP_process , 'process' , \ ; To be removed
|
|
|
|
HTTP_send , 'send' , \
|
|
|
|
HTTP_receive , 'receive' , \
|
|
|
|
HTTP_disconnect , 'disconnect' , \
|
2013-11-11 15:35:18 +04:00
|
|
|
HTTP_free , 'free' , \
|
2014-07-16 18:11:19 +04:00
|
|
|
HTTP_stop , 'stop' , \ ; To be removed
|
2014-04-14 18:39:53 +04:00
|
|
|
HTTP_escape , 'escape' , \
|
|
|
|
HTTP_unescape , 'unescape'
|
2013-11-04 17:07:21 +04:00
|
|
|
; HTTP_put , 'put' , \
|
|
|
|
; HTTP_delete , 'delete' , \
|
|
|
|
; HTTP_trace , 'trace' , \
|
|
|
|
; HTTP_connect , 'connect' , \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
section '.data' data readable writable align 16
|
|
|
|
|
|
|
|
inifile db '/sys/settings/network.ini', 0
|
|
|
|
|
|
|
|
sec_proxy:
|
|
|
|
key_proxy db 'proxy', 0
|
|
|
|
key_proxyport db 'port', 0
|
|
|
|
key_user db 'user', 0
|
|
|
|
key_password db 'password', 0
|
|
|
|
|
|
|
|
str_http11 db ' HTTP/1.1', 13, 10, 'Host: '
|
|
|
|
.length = $ - str_http11
|
2013-11-05 15:22:31 +04:00
|
|
|
str_post_cl db 13, 10, 'Content-Length: '
|
|
|
|
.length = $ - str_post_cl
|
|
|
|
str_post_ct db 13, 10, 'Content-Type: '
|
|
|
|
.length = $ - str_post_ct
|
2013-11-04 17:07:21 +04:00
|
|
|
str_proxy_auth db 13, 10, 'Proxy-Authorization: Basic '
|
|
|
|
.length = $ - str_proxy_auth
|
2015-03-20 15:04:14 +03:00
|
|
|
str_close db 'User-Agent: KolibriOS libHTTP/1.1', 13, 10, 'Connection: Close', 13, 10, 13, 10
|
|
|
|
.length = $ - str_close
|
|
|
|
str_keep db 'User-Agent: KolibriOS libHTTP/1.1', 13, 10, 'Connection: Keepalive', 13, 10, 13, 10
|
2015-08-24 16:43:34 +03:00
|
|
|
.length = $ - str_keep
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-15 11:49:31 +04:00
|
|
|
str_http db 'http://', 0
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
base64_table db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
|
|
|
|
db '0123456789+/'
|
|
|
|
|
|
|
|
str_cl db 'content-length', 0
|
2013-11-04 20:14:13 +04:00
|
|
|
str_slash db '/', 0
|
2013-11-04 17:07:21 +04:00
|
|
|
str_te db 'transfer-encoding', 0
|
2013-11-05 15:22:31 +04:00
|
|
|
str_get db 'GET ', 0
|
|
|
|
str_head db 'HEAD ', 0
|
|
|
|
str_post db 'POST ', 0
|
2013-11-04 17:07:21 +04:00
|
|
|
|
2013-11-11 21:28:49 +04:00
|
|
|
bits_must_escape:
|
|
|
|
dd 0xffffffff ; 00-1F
|
|
|
|
dd 1 shl 0 + 1 shl 2 + 1 shl 3 + 1 shl 5 + 1 shl 28 + 1 shl 30 ; "#%<>
|
|
|
|
dd 1 shl 27 + 1 shl 28 + 1 shl 29 + 1 shl 30 ;[\]^
|
|
|
|
dd 1 shl 0 + 1 shl 27 + 1 shl 28 + 1 shl 29 + 1 shl 31 ;`{|} DEL
|
|
|
|
|
|
|
|
dd 0xffffffff
|
|
|
|
dd 0xffffffff
|
|
|
|
dd 0xffffffff
|
|
|
|
dd 0xffffffff
|
|
|
|
|
|
|
|
str_hex:
|
|
|
|
db '0123456789ABCDEF'
|
|
|
|
|
2013-11-04 17:07:21 +04:00
|
|
|
include_debug_strings
|
|
|
|
|
|
|
|
; uninitialized data
|
|
|
|
mem.alloc dd ?
|
|
|
|
mem.free dd ?
|
|
|
|
mem.realloc dd ?
|
|
|
|
dll.load dd ?
|
|
|
|
|
|
|
|
proxyAddr rb 256
|
|
|
|
proxyUser rb 256
|
|
|
|
proxyPassword rb 256
|
|
|
|
proxyPort dd ?
|