Subversion Repositories Kolibri OS

Rev

Rev 661 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1645 clevermous 1
; wget 0.2 by barsuk
2
; based on Menuet Httpc
31 halyavin 3
 
4
 
5
; Enabling debugging puts stuff to the debug board
6
DEBUGGING_ENABLED           equ     1
7
DEBUGGING_DISABLED          equ     0
1645 clevermous 8
DEBUGGING_STATE             equ     DEBUGGING_ENABLED
31 halyavin 9
 
10
use32
661 ataualpa 11
 org    0x0
12
 db     'MENUET01'    ; header
13
 dd     0x01          ; header version
14
 dd     START         ; entry point
15
 dd     I_END         ; image size
16
 dd     0x100000      ; required memory
17
 dd     0x100000      ; esp
1645 clevermous 18
 dd     params		  ; I_PARAM
19
 dd		0x0     	  ;I_Path
31 halyavin 20
 
21
include 'lang.inc'
1645 clevermous 22
include '../../../macros.inc'
23
include "DEBUG.INC"
31 halyavin 24
 
1645 clevermous 25
URLMAXLEN       equ     256  ; maximum length of url string
31 halyavin 26
 
1645 clevermous 27
primary_buffer_size	equ	4096
28
 
31 halyavin 29
; Memory usage
1645 clevermous 30
; webpage headers at buf_headers
31 halyavin 31
 
32
START:                              ; start of execution
33
 
34
;dps <"Program started",13,10>
35
 
36
    mov     eax,40                          ; Report events
37
    mov     ebx,10000111b                   ; Stack 8 + defaults
485 heavyiron 38
    mcall
1645 clevermous 39
 
40
	mov		eax, 68						; create local heap
41
	mov		ebx, 11
42
	mcall
43
 
44
	call		load_settings
45
 
46
	cmp		byte [params], 0
47
	jz		red
48
 
49
	; we have an url
50
 
51
	mov		edi, document_user
52
	mov		al, ' '
53
	mov		ecx, URLMAXLEN
54
	rep		stosb
55
 
56
	mov		esi, params
57
	mov		edi, document_user
58
.copy_param:
59
	mov		al, [esi]
60
	cmp		al, 0
61
	jz		.done
62
	cmp		al, ' '
63
	jz		.done_inc
64
	mov		[edi], al
65
	inc		esi
66
	inc		edi
67
	jmp		.copy_param
68
.done_inc:
69
	; url is followed by shared memory name.
70
	inc		esi
71
.done:
72
	mov		[shared_name], esi
31 halyavin 73
 
1645 clevermous 74
	mov		ah, 22			; strange way to tell that socket should be opened...
75
	call	socket_commands
76
 
77
	jmp		still
78
 
485 heavyiron 79
red:                    ; redraw
31 halyavin 80
    call    draw_window
81
 
82
still:
83
    mov     eax,23                 ; wait here for event
84
    mov     ebx,1
485 heavyiron 85
    mcall
31 halyavin 86
 
87
    cmp     eax,1                  ; redraw request ?
88
    je      red
89
    cmp     eax,2                  ; key in buffer ?
90
    je      key
91
    cmp     eax,3                  ; button in buffer ?
92
    je      button
93
 
94
    ; Get the web page data from the remote server
95
    call    read_incoming_data
1645 clevermous 96
 
31 halyavin 97
    mov     eax,[status]
98
    mov     [prev_status],eax
99
 
100
    mov     eax,53
101
    mov     ebx,6
102
    mov     ecx,[socket]
485 heavyiron 103
    mcall
31 halyavin 104
 
105
    mov     [status],eax
106
 
107
    cmp     [prev_status],4
108
    jge     no_send
109
    cmp     [status],4
110
    jne     no_send
111
 
112
    mov     [onoff],1
113
 
114
    call    send_request
115
 
116
no_send:
117
    call    print_status
118
 
119
    cmp     [prev_status],4
120
    jne     no_close
121
    cmp     [status],4   ; connection closed by server
122
    jbe     no_close     ; respond to connection close command
123
                         ; draw page
124
 
125
    call    read_incoming_data
126
 
127
    mov     eax,53
128
    mov     ebx,8
129
    mov     ecx,[socket]
485 heavyiron 130
    mcall
31 halyavin 131
 
132
    call    draw_page
133
 
134
    mov     [onoff],0
135
 
136
no_close:
137
    jmp     still
138
 
139
key:                    ; key
140
    mov     eax,2       ; just read it and ignore
485 heavyiron 141
    mcall
31 halyavin 142
    shr     eax,8
143
    cmp     eax,184
144
    jne     no_down
145
    cmp     [display_from],25
146
    jb      no_down
147
    sub     [display_from],25
148
    call    display_page
149
 
150
no_down:
151
    cmp     eax,183
152
    jne     no_up
153
    add     [display_from],25
154
    call    display_page
155
 
156
no_up:
157
    jmp     still
158
 
159
button:                 ; button
160
;dps <"Button pressed",13,10>
161
    mov     eax,17      ; get id
485 heavyiron 162
    mcall
1645 clevermous 163
	cmp		ah,26
164
	je		save
31 halyavin 165
    cmp     ah,1                   ; button id=1 ?
166
    jne     noclose
167
 
168
;dps "Closing socket before exit... "
169
 
1645 clevermous 170
close_end_exit:
31 halyavin 171
 
172
;dpd eax
173
;dps <13,10>
174
 
175
exit:
176
    or      eax,-1                 ; close this program
485 heavyiron 177
    mcall
31 halyavin 178
 
1645 clevermous 179
save:
180
dps "saving"
181
newline
182
	mov		eax, 70
183
	mov		ebx, fileinfo
184
	mcall
185
;pregs
186
 
187
	jmp		still
188
 
31 halyavin 189
noclose:
190
    cmp     ah,31
191
    jne     noup
192
    sub     [display_from],20
193
    call    display_page
194
    jmp     still
195
 
196
noup:
197
    cmp     ah,32
198
    jne     nodown
199
    add     [display_from],20
200
    call    display_page
201
    jmp     still
202
 
203
nodown:
204
    cmp     ah, 10              ; Enter url
205
    jne     nourl
206
 
207
    mov     [addr],dword document_user
208
    mov     [ya],dword 38
209
    mov     [len],dword URLMAXLEN
210
 
1645 clevermous 211
    mov     ecx,URLMAXLEN
31 halyavin 212
    mov     edi,[addr]
213
    mov     al,' '
214
    rep     stosb
215
 
216
    call    print_text
217
 
218
    mov     edi,[addr]
219
 
220
f11:
221
    mov     eax,10
485 heavyiron 222
    mcall
31 halyavin 223
    cmp     eax,2 ; key?
224
    jz      fbu
225
    jmp     still
226
 
227
fbu:
228
    mov     eax,2
485 heavyiron 229
    mcall  ; get key
31 halyavin 230
    shr     eax,8
231
    cmp     eax,8
232
    jnz     nobs
233
    cmp     edi,[addr]
234
    jz      f11
235
    sub     edi,1
236
    mov     [edi],byte ' '
237
    call    print_text
238
    jmp     f11
239
 
240
nobs:
241
    cmp     eax, dword 10
242
    je      retkey
243
    cmp     eax, dword 13
244
    je      retkey
245
 
246
    cmp     eax,dword 31
247
    jbe     f11
248
 
249
; Removed in v0.4
250
;    cmp     eax,dword 95
251
;    jb      keyok
252
;    sub     eax,32
253
 
254
keyok:
255
    mov     [edi],al
256
 
257
    call    print_text
258
 
259
    add     edi,1
260
    mov     esi,[addr]
261
    add     esi,URLMAXLEN
262
    cmp     esi,edi
263
    jnz     f11
264
 
265
    jmp  still
266
 
267
retkey:
268
    mov     ah, 22   ; start load
269
 
270
nourl:
271
    call    socket_commands     ; opens or closes the connection
272
    jmp     still
273
 
274
 
275
;****************************************************************************
276
;    Function
277
;       send_request
278
;
279
;   Description
280
;       Transmits the GET request to the server.
1645 clevermous 281
;       This is done as GET then URL then HTTP/1.1',13,10,13,10 in 3 packets
31 halyavin 282
;
283
;****************************************************************************
284
send_request:
285
    pusha
1645 clevermous 286
 
287
	mov		esi, string0
288
	mov		edi, request
289
	movsd
31 halyavin 290
 
1645 clevermous 291
    ; If proxy is used, make absolute URI - prepend http://
292
	cmp		byte [proxyAddr], 0
293
	jz		.noproxy
294
	mov		dword [edi], 'http'
295
	mov		byte [edi+4], ':'
296
	mov		word [edi+5], '//'
297
	add		edi, 7
298
	mov		esi, webAddr
299
.copy_host_loop:
300
	lodsb
301
	cmp		al, ' '
302
	jz		.noproxy
303
	stosb
304
	jmp		.copy_host_loop
305
.noproxy:
306
 
307
	mov		edx, 0
308
.next_edx:
31 halyavin 309
    ; Determine the length of the url to send in the GET request
1645 clevermous 310
	mov		al, [edx+document]
311
	cmp		al, ' '
312
    je     	.document_done
313
	mov		[edi], al
314
	inc		edi
31 halyavin 315
    inc     edx
1645 clevermous 316
	jmp		.next_edx
317
.document_done:
31 halyavin 318
 
1645 clevermous 319
	mov		esi, stringh
320
	mov		ecx, stringh_end-stringh
321
	rep		movsb
31 halyavin 322
 
1645 clevermous 323
	mov		edx, 0
324
.webaddr_next:
325
	mov		al, [webAddr + edx]
326
	cmp		al, ' '
327
	je		.webaddr_done
328
	mov		[edi], al
329
	inc		edi
330
	inc		edx
331
	jmp		.webaddr_next
332
.webaddr_done:
31 halyavin 333
 
1645 clevermous 334
	cmp		byte [proxyUser], 0
335
	jz		@f
336
	call		append_proxy_auth_header
337
@@:
338
 
339
	mov		esi, connclose
340
	mov		ecx, connclose_end-connclose
341
	rep		movsb
342
 
343
	pusha
344
	mov  eax,63
345
	mov  ebx,1
346
	mov  edx, request
347
@@:
348
   mov  cl,[edx]
349
   cmp edx,edi
350
   jz   @f
351
   int  40h
352
   inc  edx
353
   jmp  @b
354
@@:
355
	popa
356
 
357
    mov     edx, edi
358
	sub		edx, request
359
 
360
	;;;;now write \r\nConnection: Close \r\n\r\n
361
    mov     eax, 53     ; ' HTTP/1.1 .. '
362
    mov     ebx, 7
363
    mov     ecx, [socket]
364
    mov     esi, request
365
    mcall
366
 
31 halyavin 367
    popa
368
    ret
369
 
370
 
371
;****************************************************************************
372
;    Function
373
;       print_status
374
;
375
;   Description
376
;       displays the socket/data received status information
377
;
378
;****************************************************************************
379
print_status:
380
    pusha
381
 
382
    mov     eax,26
383
    mov     ebx,9
485 heavyiron 384
    mcall
31 halyavin 385
 
386
    cmp     eax,[nextupdate]
387
    jb      status_return
388
 
389
    add     eax,25
390
 
391
    mov     [nextupdate],eax
392
 
393
    mov     eax,13
394
    mov     ebx,5*65536+100
395
    mov     ecx,[winys]
396
    shl     ecx,16
397
    add     ecx,-18*65536+10
398
    mov     edx,0xffffff
485 heavyiron 399
    mcall
31 halyavin 400
 
401
    mov     eax,47
402
    mov     ebx,3*65536
403
    mov     ecx,[status]
404
    mov     edx,12*65536-18
405
    add     edx,[winys]
406
    mov     esi,0x000000
485 heavyiron 407
    mcall
31 halyavin 408
 
409
    mov     eax,47
410
    mov     ebx,6*65536
411
    mov     ecx,[pos]
412
    mov     edx,40*65536-18
413
    add     edx,[winys]
414
    mov     esi,0x000000
485 heavyiron 415
    mcall
31 halyavin 416
 
417
status_return:
418
    popa
419
    ret
420
 
421
 
422
;****************************************************************************
423
;    Function
424
;       read_incoming_data
425
;
426
;   Description
427
;       receive the web page from the server, storing it without processing
428
;
429
;****************************************************************************
430
read_incoming_data:
431
    cmp     [onoff],1
432
    je      rid
433
    ret
434
 
435
rid:
436
 
1645 clevermous 437
	push	esi
438
	push	edi
439
dps "rid"
440
newline
441
 
31 halyavin 442
newbyteread:
1645 clevermous 443
 
444
	;call	print_status
445
 
31 halyavin 446
    mov     eax, 53
447
    mov     ebx, 2
448
    mov     ecx, [socket]
485 heavyiron 449
    mcall
31 halyavin 450
 
451
    cmp     eax,0
452
    je      no_more_data
453
 
454
    mov     eax, 53
1645 clevermous 455
    mov     ebx, 11
456
	mov		edx, primary_buf
31 halyavin 457
    mov     ecx, [socket]
1645 clevermous 458
	mov		esi, primary_buffer_size
485 heavyiron 459
    mcall
1645 clevermous 460
 
461
;dps "part "
462
;dph eax
463
;newline
31 halyavin 464
 
1645 clevermous 465
	mov		edi, [pos]
31 halyavin 466
 
1645 clevermous 467
	add		[pos], eax
468
	push	eax
469
 
470
	mov		eax, 68
471
	mov		ebx, 20
472
	mov		ecx, [pos]
473
	mov		edx, [buf_ptr]
474
	mcall
475
 
476
	mov		[buf_ptr], eax
477
	add		edi, eax
478
	mov		esi, primary_buf
479
	pop		ecx					; number of recently read bytes
480
	lea		edx, [ecx - 3]
481
	rep		movsb
482
 
31 halyavin 483
no_more_data:
484
 
1645 clevermous 485
	mov		eax, 53
486
	mov		ebx, 6
487
	mov		ecx, [socket]
488
	mcall
489
	cmp		eax, 4
490
	jne		no_more_data.finish
31 halyavin 491
 
1645 clevermous 492
	jmp		newbyteread
31 halyavin 493
 
1645 clevermous 494
.finish:
31 halyavin 495
 
1645 clevermous 496
;dps "finish "
497
;pregs
31 halyavin 498
 
1645 clevermous 499
	call	parse_result
31 halyavin 500
 
1645 clevermous 501
	mov		ecx, [shared_name]
502
	cmp		byte [ecx], 0
503
	jnz		save_in_shared
504
	mov		eax, 70
505
	mov		ebx, fileinfo
506
	mcall
507
;dps "saving "
508
;pregs
31 halyavin 509
 
1645 clevermous 510
	;jmp		close_end_exit
511
 
512
	pop		edi
513
	pop		esi
514
 
515
	; if called from command line, then exit
516
	cmp		byte [params], 0
517
	jnz		exit
518
 
519
    ret
520
 
521
save_in_shared:
522
	mov	eax, 68
523
	mov	ebx, 22
524
	mov	esi, 1	; SHM_OPEN+SHM_WRITE
525
	mcall
526
	test	eax, eax
527
	jz	save_in_shared_done
528
	sub	edx, 4
529
	jbe	save_in_shared_done
530
	mov	ecx, [final_size]
531
	cmp	ecx, edx
532
	jb	@f
533
	mov	ecx, edx
534
@@:
535
	mov	[eax], ecx
536
	lea	edi, [eax+4]
537
	mov	esi, [final_buffer]
538
	mov	edx, ecx
539
	shr	ecx, 2
540
	rep	movsd
541
	mov	ecx, edx
542
	and	ecx, 3
543
	rep	movsb
544
save_in_shared_done:
545
	pop	edi
546
	pop	esi
547
	jmp	exit
548
 
549
	; this function cuts header, and removes chunk sizes if doc is chunked
550
	; in: buf_ptr, pos; out: buf_ptr, pos.
551
 
552
parse_result:
31 halyavin 553
 
1645 clevermous 554
	; close socket
555
    mov     eax, 53
556
    mov     ebx, 8
557
    mov     ecx, [socket]
558
    mcall
559
 
560
dps "close socket: "
561
dph eax
562
newline
31 halyavin 563
 
1645 clevermous 564
	mov		edi, [buf_ptr]
565
	mov		edx, [pos]
566
 
567
	mov		[buf_size], edx
568
	;mov		eax, 70
569
	;mov		ebx, fileinfo_tmp
570
	;mcall
31 halyavin 571
 
1645 clevermous 572
dps "pos = "
573
dph edx
574
newline
31 halyavin 575
 
1645 clevermous 576
	; first, find end of headers
577
 
578
.next_byte:
579
	cmp		dword [edi], 0x0d0a0d0a		; мне лень читать стандарт, пусть будут оба варианта
580
	je		.end_of_headers
581
	cmp		dword [edi], 0x0a0d0a0d
582
	je		.end_of_headers
583
	inc		edi
584
	dec		edx
585
	jne		.next_byte
586
 
587
	; no end of headers. it's an error. let client see all those headers.
588
	ret
589
 
590
.end_of_headers:
31 halyavin 591
 
592
 
1645 clevermous 593
	; here we look at headers and search content-length or transfer-encoding headers
31 halyavin 594
 
1645 clevermous 595
;dps "eoh "
596
;newline
31 halyavin 597
 
1645 clevermous 598
	sub		edi, [buf_ptr]
599
	add		edi, 3
600
	mov		[body_pos], edi		; store position where document body starts
601
	mov		[is_chunked], 0
31 halyavin 602
 
1645 clevermous 603
	; find content-length in headers
604
	; not good method, but should work for 'Content-Length:'
605
 
606
	mov		esi, [buf_ptr]
607
	mov		edi, s_contentlength
608
	mov		ebx, [body_pos]
609
	mov		edx, 0
610
.cl_next:
611
	mov		al, [esi]
612
	cmp		al, [edi + edx]
613
	jne		.cl_fail
614
	inc		edx
615
	cmp		edx, len_contentlength
616
	je		.cl_found
617
	jmp		.cl_incr
618
.cl_fail:
619
	mov		edx, 0
620
.cl_incr:
621
	inc		esi
622
	dec		ebx
623
	je		.cl_error
624
	jmp		.cl_next
625
.cl_error:
626
;pregs
627
;newline
628
;dph esi
629
;dps	" content-length not found "
31 halyavin 630
 
1645 clevermous 631
	; find 'chunked'
632
 
633
	; да, я копирую код, это ужасно, но мне хочется, чтобы поскорее заработало
634
	; а там уж отрефакторю
635
 
636
	mov		esi, [buf_ptr]
637
	mov		edi, s_chunked
638
	mov		ebx, [body_pos]
639
	mov		edx, 0
640
.ch_next:
641
	mov		al, [esi]
642
	cmp		al, [edi + edx]
643
	jne		.ch_fail
644
	inc		edx
645
	cmp		edx, len_chunked
646
	je		.ch_found
647
	jmp		.ch_incr
648
.ch_fail:
649
	mov		edx, 0
650
.ch_incr:
651
	inc		esi
652
	dec		ebx
653
	je		.ch_error
654
	jmp		.ch_next
655
.ch_error:
31 halyavin 656
 
1645 clevermous 657
	; if neither of the 2 headers is found, it's an error
658
;dps "transfer-encoding: chunked not found "
659
	mov		eax, [pos]
660
	sub		eax, [body_pos]
661
	jmp		.write_final_size
31 halyavin 662
 
1645 clevermous 663
.ch_found:
31 halyavin 664
 
1645 clevermous 665
	mov		[is_chunked], 1
666
	mov		eax, [body_pos]
667
	add		eax, [buf_ptr]
668
	sub		eax, 2
669
	mov		[prev_chunk_end], eax
670
	jmp		parse_chunks
671
 
672
.cl_found:
673
 
674
	call	read_number		; eax = number from *esi
675
	inc		eax				; bugfix :)
676
 
677
.write_final_size:
678
	mov		[final_size], eax		; if this works, i will b very happy...
679
 
680
	mov		ebx, [pos]	; we well check if it is right
681
	sub		ebx, [body_pos]
31 halyavin 682
 
1645 clevermous 683
;dps "check cl eax==ebx "
684
;pregs
31 halyavin 685
 
1645 clevermous 686
	; everything is ok, so we return
687
 
688
	mov		eax, [body_pos]
689
	mov		ebx, [buf_ptr]
690
	add		ebx, eax
691
	mov		[final_buffer], ebx
692
	;mov		ebx, [pos]
693
	;sub		ebx, eax
694
	;mov		[final_size], ebx
695
 
696
	ret
697
 
698
parse_chunks:
31 halyavin 699
 
1645 clevermous 700
;dps "parse chunks"
701
;newline
702
	; we have to look through the data and remove sizes of chunks we see
703
 
704
	; 1. read size of next chunk
705
	; 2. if 0, it's end. if not, continue.
706
	; 3. make a good buffer and copy a chunk there
707
 
708
	mov		[final_buffer], 0
709
	mov		[final_size], 0
710
 
711
.read_size:
712
 
713
	mov		eax, [prev_chunk_end]
714
	mov		ebx, eax
715
	sub		ebx, [buf_ptr]
31 halyavin 716
 
1645 clevermous 717
	mov		edx, eax
718
 
719
;dps "rs "
720
;pregs
31 halyavin 721
 
1645 clevermous 722
	cmp		ebx, [pos]
723
	jae		chunks_end			; not good
724
 
725
	call	read_hex			; in: eax=pointer to text. out:eax=hex number,ebx=end of text.
726
	cmp		eax, 0
727
	jz		chunks_end
31 halyavin 728
 
1645 clevermous 729
	add		ebx, 1
730
	mov		edx, ebx			; edx = size of size of chunk
731
 
732
	add		ebx, eax
733
	mov		[prev_chunk_end], ebx
734
 
735
;dps "sz "
736
;pregs
31 halyavin 737
 
1645 clevermous 738
	; do copying: from buf_ptr+edx to final_buffer+prev_final_size count eax
739
 
740
	; realloc final buffer
741
 
742
	push	eax
743
	push	edx
744
	push	dword [final_size]
745
 
746
	add		[final_size], eax
747
 
748
	mov		eax, 68
749
	mov		ebx, 20
750
	mov		ecx, [final_size]
751
	mov		edx, [final_buffer]
752
	mcall
753
	mov		[final_buffer], eax
754
;dps "re "
755
;pregs
756
 
757
	pop		edi
758
	pop		esi
759
	pop		ecx
31 halyavin 760
 
1645 clevermous 761
	;add		[pos], ecx
762
 
763
	add		edi, [final_buffer]
31 halyavin 764
 
1645 clevermous 765
;dps "cp "
766
;pregs
767
 
768
	rep		movsb
769
 
770
	jmp		.read_size
771
 
772
chunks_end:
773
	; free old buffer
774
dps "chunks end"
775
newline
776
 
777
	mov		eax, 68
778
	mov		ebx, 13
779
	mov		ecx, [buf_ptr]
780
	mcall
781
 
782
	; done!
783
 
784
	ret
785
 
786
 
787
 
788
; reads content-length from [edi+ecx], result in eax
789
read_number:
31 halyavin 790
 
1645 clevermous 791
	push	ebx
792
	xor		eax, eax
793
	xor		ebx, ebx
794
.next:
795
	mov		bl, [esi]
796
;dph ebx
797
	cmp		bl, '0'
798
	jb		.not_number
799
	cmp		bl, '9'
800
	ja		.not_number
801
	sub		bl, '0'
802
	shl		eax, 1
803
	lea		eax,[eax + eax * 4]		; eax *= 10
804
	add		eax, ebx
805
.not_number:
806
	cmp		bl, 13
807
	jz		.done
808
	inc		esi
809
	jmp		.next
810
.done:
811
	pop		ebx
812
;newline
813
;dps "strtoint eax "
814
;pregs
31 halyavin 815
 
1645 clevermous 816
	ret
817
 
818
 
819
; reads hex from eax, result in eax, end of text in ebx
820
read_hex:
31 halyavin 821
 
1645 clevermous 822
	add		eax, 2
823
	mov		ebx, eax
824
 
825
	mov		eax, [ebx]
826
	mov		[deba], eax
827
 
828
;	pushf
829
;   pushad
830
;   mov  edx,deba
831
;   call debug_outstr
832
;   popad
833
;   popf
834
 
835
	xor		eax, eax
836
	xor		ecx, ecx
837
.next:
838
	mov		cl, [ebx]
839
	inc		ebx
840
 
841
	cmp		cl, 0x0d
842
	jz		.done
843
;dph ebx
844
	or		cl, 0x20
845
	sub		cl, '0'
846
	jb		.bad
847
	cmp		cl, 0x9
848
	jbe		.adding
849
	sub		cl, 'a'-'0'-10
850
	cmp		cl, 0x0a
851
	jb		.bad
852
	cmp		cl, 0x0f
853
	ja		.bad
854
.adding:
855
	shl		eax, 4
856
	or		eax, ecx
857
;	jmp		.not_number
858
;.bad:
859
.bad:
860
	jmp		.next
861
.done:
862
;newline
863
;dps "hextoint eax "
864
;pregs
865
	ret
31 halyavin 866
 
1645 clevermous 867
 
31 halyavin 868
 
869
 
1645 clevermous 870
;****************************************************************************
871
;    Function
872
;       draw_page
873
;
874
;   Description
875
;       parses the web page data, storing displayable data at 0x20000
876
;       and attributes at 0x30000. It then calls display_page to render
877
;       the data
878
;
879
;****************************************************************************
880
draw_page:
881
	ret
882
 
31 halyavin 883
 
884
;****************************************************************************
885
;    Function
886
;       linefeed
887
;
888
;   Description
889
;
890
;
891
;****************************************************************************
892
linefeed:
893
 
894
    ret
895
 
896
;****************************************************************************
897
;    Function
898
;       display_page
899
;
900
;   Description
901
;       Renders the text decoded by draw_page
902
;
903
;****************************************************************************
904
display_page:
905
    ret
906
 
907
;****************************************************************************
908
;    Function
909
;       socket_commands
910
;
911
;   Description
912
;       opens or closes the socket
913
;
914
;****************************************************************************
915
socket_commands:
916
    cmp     ah,22       ; open socket
917
    jnz     tst3
918
 
1645 clevermous 919
	dps "opening socket"
920
	newline
31 halyavin 921
    ; Clear all page memory
1645 clevermous 922
 
923
	mov		[prev_chunk_end], 0
924
 
925
	cmp		[buf_ptr], 0
926
	jz		no_free
927
	mov		eax, 68		; free buffer
928
	mov		ebx, 13
929
	mov		ecx, [buf_ptr]
930
	mcall
931
no_free:
932
	mov		[buf_size], 0
31 halyavin 933
 
934
    ; Parse the entered url
935
    call    parse_url
936
 
937
    ; Get a free port number
938
        mov         ecx, 1000           ; local port starting at 1000
939
getlp1:
940
        inc         ecx
941
        push    ecx
942
        mov         eax, 53
943
        mov         ebx, 9
485 heavyiron 944
        mcall
31 halyavin 945
        pop         ecx
946
        cmp         eax, 0                      ; is this local port in use?
947
        jz              getlp1              ; yes - so try next
948
 
949
    mov     eax,53
950
    mov     ebx,5
951
    mov     edx,80
1645 clevermous 952
    cmp     [proxyAddr], 0
953
    jz      sc000
954
    mov     edx,[proxyPort]
955
sc000:
31 halyavin 956
    mov     esi,dword [server_ip]
957
    mov     edi,1
485 heavyiron 958
    mcall
31 halyavin 959
    mov     [socket], eax
960
 
961
    mov     [pos],0
962
    mov     [pagex],0
963
    mov     [pagey],0
964
    mov     [pagexs],80
965
    mov     [command_on_off],0
1645 clevermous 966
 
967
	mov		[is_body], 0
31 halyavin 968
 
969
    ret
970
 
971
tst3:
972
    cmp     ah,24     ; close socket
973
    jnz     no_24
974
 
975
    mov     eax,53
976
    mov     ebx,8
977
    mov     ecx,[socket]
485 heavyiron 978
    mcall
31 halyavin 979
 
980
    call    draw_page
981
 
982
    ret
983
 
984
no_24:
985
    ret
986
 
987
 
988
 
989
;****************************************************************************
990
;    Function
991
;       parse_url
992
;
993
;   Description
994
;       parses the full url typed in by the user into a web address ( that
995
;       can be turned into an IP address by DNS ) and the page to display
996
;       DNS will be used to translate the web address into an IP address, if
997
;       needed.
998
;       url is at document_user and will be space terminated.
999
;       web address goes to webAddr and is space terminated.
1000
;       ip address goes to server_ip
1001
;       page goes to document and is space terminated.
1002
;
1003
;       Supported formats:
1004
;       address
1005
;        is optional, removed and ignored - only http supported
1006
;       
is required. It can be an ip address or web address
1007
;        is optional and must start with a leading / character
1008
;
1009
;****************************************************************************
1010
parse_url:
1011
    ; First, reset destination variables
1012
    cld
1013
    mov     al, ' '
1014
    mov     edi, document
1015
    mov     ecx,URLMAXLEN
1016
    rep     stosb
1017
    mov     edi, webAddr
1018
    mov     ecx,URLMAXLEN
1019
    rep     stosb
1645 clevermous 1020
 
31 halyavin 1021
    mov     al, '/'
1022
    mov     [document], al
1023
 
1024
    mov     esi, document_user
1025
    ; remove any leading protocol text
1026
    mov     ecx, URLMAXLEN
1027
    mov     ax, '//'
1028
 
1029
pu_000:
1030
    cmp     [esi], byte ' '     ; end of text?
1031
    je      pu_002              ; yep, so not found
1032
    cmp     word [esi], ax
1033
    je      pu_001              ; Found it, so esi+2 is start
1034
    inc     esi
1035
    loop    pu_000
1036
 
1037
pu_002:
1038
    ; not found, so reset esi to start
1039
    mov     esi, document_user -2
1040
 
1041
pu_001:
1042
    add     esi, 2
1043
 
1044
    mov     ebx, esi    ; save address of start of web address
1045
    mov     edi, document_user + URLMAXLEN  ; end of string
1046
 
1047
    ; look for page delimiter - it's a '/' character
1048
pu_003:
1049
    cmp     [esi], byte ' '     ; end of text?
1050
    je      pu_004              ; yep, so none found
1051
    cmp     esi, edi            ; end of string?
1052
    je      pu_004              ; yep, so none found
1053
    cmp     [esi], byte '/'     ; delimiter?
1054
    je      pu_005              ; yep - process it
1055
    inc     esi
1056
    jmp     pu_003
1057
 
1058
pu_005:
1059
    ; copy page to document address
1060
    ; esi = delimiter
1061
    push    esi
1062
    mov     ecx, edi            ; end of document_user
1063
    mov     edi, document
1064
    cld
1065
 
1066
pu_006:
1067
    movsb
1068
    cmp     esi, ecx
1069
    je      pu_007              ; end of string?
1070
    cmp     [esi], byte ' '     ; end of text
1071
    je      pu_007
1072
    jmp     pu_006
1073
 
1074
pu_007:
1075
    pop     esi                 ; point esi to '/' delimiter
1076
 
1077
pu_004:
1078
    ; copy web address to webAddr
1079
    ; start in ebx, end in esi-1
1080
    mov     ecx, esi
1081
    mov     esi, ebx
1082
    mov     edi, webAddr
1083
    cld
1084
 
1085
pu_008:
1086
    movsb
1087
    cmp     esi, ecx
1088
    je      pu_009
1089
    jmp     pu_008
1090
 
1091
pu_009:
1092
    ; For debugging, display resulting strings
1093
 
1094
if DEBUGGING_STATE = DEBUGGING_ENABLED
1095
    mov     esi, document_user
1096
    call    debug_print_string
1097
    mov     esi, webAddr
1098
    call    debug_print_string
1099
    mov     esi, document
1100
    call    debug_print_string
1101
end if
1102
 
1103
    ; Look up the ip address, or was it specified?
1645 clevermous 1104
    mov     al, [proxyAddr]
1105
    cmp     al, 0
1106
    jnz     pu_015
31 halyavin 1107
    mov     al, [webAddr]
1645 clevermous 1108
pu_015:
31 halyavin 1109
    cmp     al, '0'
1110
    jb      pu_010              ; Resolve address
1111
    cmp     al, '9'
1112
    ja      pu_010              ; Resolve address
1113
 
1114
 
1115
if DEBUGGING_STATE = DEBUGGING_ENABLED
1116
    mov     esi, str2       ; print gotip
1117
    call    debug_print_string
1118
end if
1119
 
1120
    ; Convert address
1645 clevermous 1121
    ; If proxy is given, get proxy address instead of server
1122
    mov     esi,proxyAddr-1
1123
    cmp     byte [esi+1], 0
1124
    jnz     pu_020
31 halyavin 1125
    mov     esi,webAddr-1
1645 clevermous 1126
pu_020:
1127
 
31 halyavin 1128
    mov     edi,server_ip
1129
    xor     eax,eax
1130
ip1:
1131
    inc     esi
1132
    cmp     [esi],byte '0'
1133
    jb      ip2
1134
    cmp     [esi],byte '9'
1135
    jg      ip2
1136
    imul    eax,10
1137
    movzx   ebx,byte [esi]
1138
    sub     ebx,48
1139
    add     eax,ebx
1140
    jmp     ip1
1141
ip2:
1142
    mov     [edi],al
1143
    xor     eax,eax
1144
    inc     edi
1145
    cmp     edi,server_ip+3
1146
    jbe     ip1
1147
 
1148
    jmp     pu_011
1149
 
1150
pu_010:
1151
 
1152
if DEBUGGING_STATE = DEBUGGING_ENABLED
1153
    mov     esi, str1       ; print resolving
1154
    call    debug_print_string
1155
end if
1156
 
1157
    ; Resolve Address
1158
    call    translateData       ; Convert domain & DNS IP address
1159
    call    resolveDomain       ; get ip address
1160
 
1161
if DEBUGGING_STATE = DEBUGGING_ENABLED
1162
    mov     esi, str3
1163
    call    debug_print_string
1164
end if
1165
 
1166
pu_011:
1167
 
1168
    ; Done
1169
    ret
1170
 
1171
 
1172
;***************************************************************************
1173
;   Function
1174
;      translateData
1175
;
1176
;   Description
1177
;      Coverts the domain name and DNS IP address typed in by the user into
1178
;      a format suitable for the IP layer.
1179
;
1180
;    The ename, in query, is converted and stored in dnsMsg
1181
;
1182
;***************************************************************************
1183
translateData:
1184
 
1185
    ; first, get the IP address of the DNS server
1186
    ; Then, build up the request string.
1187
 
1188
 
1189
    ; Build the request string
1190
 
1191
 
1192
    mov     eax, 0x00010100
1193
    mov     [dnsMsg], eax
1194
    mov     eax, 0x00000100
1195
    mov     [dnsMsg+4], eax
1196
    mov     eax, 0x00000000
1197
    mov     [dnsMsg+8], eax
1198
 
1199
    ; domain name goes in at dnsMsg+12
1200
    mov     esi, dnsMsg + 12        ; location of label length
1201
    mov     edi, dnsMsg + 13        ; label start
1645 clevermous 1202
    mov     edx, proxyAddr
1203
    cmp     byte [edx], 0
1204
    jnz     td000
31 halyavin 1205
    mov     edx, webAddr
1645 clevermous 1206
td000:
31 halyavin 1207
    mov     ecx, 12                  ; total string length so far
1208
 
1209
td002:
1210
    mov     [esi], byte 0
1211
    inc     ecx
1212
 
1213
td0021:
1214
    mov     al, [edx]
1215
    cmp     al, ' '
1216
    je      td001                   ; we have finished the string translation
1645 clevermous 1217
    cmp     al, 0
1218
    je      td001
31 halyavin 1219
    cmp     al, '.'                 ; we have finished the label
1220
    je      td004
1221
 
1222
    inc     byte [esi]
1223
    inc     ecx
1224
    mov     [edi], al
1225
    inc     edi
1226
    inc     edx
1227
    jmp     td0021
1228
 
1229
td004:
1230
    mov     esi, edi
1231
    inc     edi
1232
    inc     edx
1233
    jmp     td002
1234
 
1235
 
1236
 
1237
    ; write label len + label text
1238
 
1239
td001:
1240
    mov     [edi], byte 0
1241
    inc     ecx
1242
    inc     edi
1243
    mov     [edi], dword 0x01000100
1244
    add     ecx, 4
1245
 
1246
    mov     [dnsMsgLen], ecx
1247
 
1248
    ret
1249
 
1250
 
1251
 
1252
 
1253
 
1254
;***************************************************************************
1255
;   Function
1256
;      resolveDomain
1257
;
1258
;   Description
1259
;       Sends a question to the dns server
1260
;       works out the IP address from the response from the DNS server
1261
;
1262
;***************************************************************************
1263
resolveDomain:
1264
    ; Get a free port number
1265
        mov         ecx, 1000           ; local port starting at 1000
1266
getlp:
1267
        inc         ecx
1268
        push    ecx
1269
        mov         eax, 53
1270
        mov         ebx, 9
485 heavyiron 1271
        mcall
31 halyavin 1272
        pop         ecx
1273
        cmp         eax, 0                      ; is this local port in use?
1274
        jz              getlp               ; yes - so try next
1275
 
1645 clevermous 1276
    ; Get DNS IP
1277
    mov     eax, 52
1278
    mov     ebx, 13
1279
    mcall
1280
    mov     esi, eax
31 halyavin 1281
    ; First, open socket
1282
    mov     eax, 53
1283
    mov     ebx, 0
1284
    mov     edx, 53    ; remote port - dns
1645 clevermous 1285
;    mov     esi, dword [dns_ip]
485 heavyiron 1286
    mcall
31 halyavin 1287
 
1288
    mov     [socketNum], eax
1289
 
1290
    ; write to socket ( request DNS lookup )
1291
    mov     eax, 53
1292
    mov     ebx, 4
1293
    mov     ecx, [socketNum]
1294
    mov     edx, [dnsMsgLen]
1295
    mov     esi, dnsMsg
485 heavyiron 1296
    mcall
31 halyavin 1297
 
1298
    ; Setup the DNS response buffer
1299
 
1300
    mov     eax, dnsMsg
1301
    mov     [dnsMsgLen], eax
1302
 
1303
    ; now, we wait for
1304
    ; UI redraw
1305
    ; UI close
1306
    ; or data from remote
1307
 
1308
ctr001:
1309
    mov     eax,10                 ; wait here for event
485 heavyiron 1310
    mcall
31 halyavin 1311
 
1312
    cmp     eax,1                  ; redraw request ?
1313
    je      ctr003
1314
    cmp     eax,2                  ; key in buffer ?
1315
    je      ctr004
1316
    cmp     eax,3                  ; button in buffer ?
1317
    je      ctr005
1318
 
1319
 
1320
    ; Any data in the UDP receive buffer?
1321
    mov     eax, 53
1322
    mov     ebx, 2
1323
    mov     ecx, [socketNum]
485 heavyiron 1324
    mcall
31 halyavin 1325
 
1326
    cmp     eax, 0
1327
    je      ctr001
1328
 
1329
    ; we have data - this will be the response
1330
ctr002:
1331
    mov     eax, 53
1332
    mov     ebx, 3
1333
    mov     ecx, [socketNum]
485 heavyiron 1334
    mcall                ; read byte - block (high byte)
31 halyavin 1335
 
1336
    ; Store the data in the response buffer
1337
    mov     eax, [dnsMsgLen]
1338
    mov     [eax], bl
1339
    inc     dword [dnsMsgLen]
1340
 
1341
    mov     eax, 53
1342
    mov     ebx, 2
1343
    mov     ecx, [socketNum]
485 heavyiron 1344
    mcall                ; any more data?
31 halyavin 1345
 
1346
    cmp     eax, 0
1347
    jne     ctr002              ; yes, so get it
1348
 
1349
    ; close socket
1350
    mov     eax, 53
1351
    mov     ebx, 1
1352
    mov     ecx, [socketNum]
485 heavyiron 1353
    mcall
31 halyavin 1354
 
1355
    mov     [socketNum], dword 0xFFFF
1356
 
1357
    ; Now parse the message to get the host IP
1358
    ; Man, this is complicated. It's described in
1359
    ; RFC 1035
1360
 
1361
if DEBUGGING_STATE = DEBUGGING_ENABLED
1362
    mov     esi, str4
1363
    call    debug_print_string
1364
end if
1365
 
1366
    ; 1) Validate that we have an answer with > 0 responses
1367
    ; 2) Find the answer record with TYPE 0001 ( host IP )
1368
    ; 3) Finally, copy the IP address to the display
1369
    ; Note: The response is in dnsMsg
1370
    ;       The end of the buffer is pointed to by [dnsMsgLen]
1371
 
1372
    ; Clear the IP address text
1373
    mov     [server_ip], dword 0
1374
 
1375
    mov     esi, dnsMsg
1376
 
1377
    ; Is this a response to my question?
1378
    mov     al, [esi+2]
1379
    and     al, 0x80
1380
    cmp     al, 0x80
1381
    jne     ctr002a
1382
 
1383
    ; Were there any errors?
1384
    mov     al, [esi+3]
1385
    and     al, 0x0F
1386
    cmp     al, 0x00
1387
    jne     ctr002a
1388
 
1389
    ; Is there ( at least 1 ) answer?
1390
    mov     ax, [esi+6]
1391
    cmp     ax, 0x00
1392
    je      ctr002a
1393
 
1394
    ; Header validated. Scan through and get my answer
1395
 
1396
if DEBUGGING_STATE = DEBUGGING_ENABLED
1397
    pusha
1398
    mov     esi, str4
1399
    call    debug_print_string
1400
    popa
1401
end if
1402
 
1403
    add     esi, 12             ; Skip to the question field
1404
 
1405
    ; Skip through the question field
1406
    call    skipName
1407
    add     esi, 4              ; skip past the questions qtype, qclass
1408
 
1409
ctr002z:
1410
    ; Now at the answer. There may be several answers,
1411
    ; find the right one ( TYPE = 0x0001 )
1412
    call    skipName
1413
    mov     ax, [esi]
1414
    cmp     ax, 0x0100          ; Is this the IP address answer?
1415
    jne     ctr002c
1416
 
1417
    ; Yes! Point esi to the first byte of the IP address
1418
    add     esi, 10
1419
 
1420
    mov     eax, [esi]
1421
    mov     [server_ip], eax
1422
    ret
1423
 
1424
 
1425
ctr002c:                        ; Skip through the answer, move to the next
1426
    add     esi, 8
1427
    movzx   eax, byte [esi+1]
1428
    mov     ah, [esi]
1429
    add     esi, eax
1430
    add     esi, 2
1431
 
1432
    ; Have we reached the end of the msg?
1433
    ; This is an error condition, should not happen
1434
    cmp     esi, [dnsMsgLen]
1435
    jl      ctr002z             ; Check next answer
1436
    jmp     ctr002a             ; abort
1437
 
1438
ctr002a:
1439
    jmp     ctr001
1440
 
1441
ctr003:                         ; redraw
1442
    call    draw_window
1443
    jmp     ctr001
1444
 
1445
ctr004:                         ; key
1446
    mov     eax,2               ; just read it and ignore
485 heavyiron 1447
    mcall
31 halyavin 1448
    jmp     ctr001
1449
 
1450
ctr005:                         ; button
1451
    mov     eax,17              ; get id
485 heavyiron 1452
    mcall
31 halyavin 1453
 
1454
    mov     dl, ah
1455
 
1456
    ; close socket
1457
    mov     eax, 53
1458
    mov     ebx, 1
1459
    mov     ecx, [socketNum]
485 heavyiron 1460
    mcall
31 halyavin 1461
 
1462
    cmp     dl, 1
1463
    je      exit
1464
 
1465
    mov     [socketNum], dword 0xFFFF
1466
    mov     [server_ip], dword 0
1467
 
1468
    ret
1469
 
1470
 
1471
 
1472
;***************************************************************************
1473
;   Function
1474
;      skipName
1475
;
1476
;   Description
1477
;       Increment esi to the first byte past the name field
1478
;       Names may use compressed labels. Normally do.
1479
;       RFC 1035 page 30 gives details
1480
;
1481
;***************************************************************************
1482
skipName:
1483
    mov     al, [esi]
1484
    cmp     al, 0
1485
    je      sn_exit
1486
    and     al, 0xc0
1487
    cmp     al, 0xc0
1488
    je      sn001
1489
 
1490
    movzx   eax, byte [esi]
1491
    inc     eax
1492
    add     esi, eax
1493
    jmp     skipName
1494
 
1495
sn001:
1496
    add     esi, 2                          ; A pointer is always at the end
1497
    ret
1498
 
1499
sn_exit:
1500
    inc     esi
1501
    ret
1502
 
1645 clevermous 1503
;***************************************************************************
1504
;   Function
1505
;       load_settings
1506
;
1507
;   Description
1508
;       Load settings from configuration file network.ini
1509
;
1510
;***************************************************************************
1511
include "proc32.inc"
1512
include "dll.inc"
1513
load_settings:
1514
    stdcall dll.Load, @IMPORT
1515
    test    eax, eax
1516
    jnz     ls001
1517
    invoke  ini.get_str, inifile, sec_proxy, key_proxy, proxyAddr, 256, proxyAddr
1518
    invoke  ini.get_int, inifile, sec_proxy, key_proxyport, 80
1519
    mov     [proxyPort], eax
1520
    invoke  ini.get_str, inifile, sec_proxy, key_user, proxyUser, 256, proxyUser
1521
    invoke  ini.get_str, inifile, sec_proxy, key_password, proxyPassword, 256, proxyPassword
1522
ls001:
1523
    ret
31 halyavin 1524
 
1645 clevermous 1525
;***************************************************************************
1526
;   Function
1527
;       append_proxy_auth_header
1528
;
1529
;   Description
1530
;       Append header to HTTP request for proxy authentification
1531
;
1532
;***************************************************************************
1533
append_proxy_auth_header:
1534
    mov     esi, proxy_auth_basic
1535
    mov     ecx, proxy_auth_basic_end - proxy_auth_basic
1536
    rep     movsb
1537
    ; base64-encode string :
1538
    mov     esi, proxyUser
1539
apah000:
1540
    lodsb
1541
    test    al, al
1542
    jz      apah001
1543
    call    encode_base64_byte
1544
    jmp     apah000
1545
apah001:
1546
    mov     al, ':'
1547
    call    encode_base64_byte
1548
    mov     esi, proxyPassword
1549
apah002:
1550
    lodsb
1551
    test    al, al
1552
    jz      apah003
1553
    call    encode_base64_byte
1554
    jmp     apah002
1555
apah003:
1556
    call    encode_base64_final
1557
    ret
31 halyavin 1558
 
1645 clevermous 1559
encode_base64_byte:
1560
    inc     ecx
1561
    shl     edx, 8
1562
    mov     dl, al
1563
    cmp     ecx, 3
1564
    je      ebb001
1565
    ret
1566
ebb001:
1567
    shl     edx, 8
1568
    inc     ecx
1569
ebb002:
1570
    rol     edx, 6
1571
    xor     eax, eax
1572
    xchg    al, dl
1573
    mov     al, [base64_table+eax]
1574
    stosb
1575
    loop    ebb002
1576
    ret
1577
 
1578
encode_base64_final:
1579
    mov     al, 0
1580
    test    ecx, ecx
1581
    jz      ebf000
1582
    call    encode_base64_byte
1583
    test    ecx, ecx
1584
    jz      ebf001
1585
    call    encode_base64_byte
1586
    mov     byte [edi-2], '='
1587
ebf001:
1588
    mov     byte [edi-1], '='
1589
ebf000:
1590
    ret
1591
 
31 halyavin 1592
if DEBUGGING_STATE = DEBUGGING_ENABLED
1593
 
1594
;****************************************************************************
1595
;    Function
1596
;       debug_print_string
1597
;
1598
;   Description
1599
;       prints a string to the debug board, in quotes
1600
;
1601
;       esi holds ptr to msg to display, which is space or 0 terminated
1602
;
1603
;       Nothing preserved; I'm assuming a pusha/popa is done before calling
1604
;
1605
;****************************************************************************
1606
debug_print_string:
1607
    push    esi
1608
    mov     cl, '"'
1609
    mov     eax,63
1610
    mov     ebx, 1
485 heavyiron 1611
    mcall
31 halyavin 1612
    pop     esi
1613
 
1614
dps_000:
1615
    mov     cl, [esi]
1616
    cmp     cl, 0
1617
    je      dps_exit
1618
    cmp     cl, ' '
1619
    je      dps_exit
1620
    jmp     dps_001
1621
 
1622
dps_exit:
1623
    mov     cl, '"'
1624
    mov     eax,63
1625
    mov     ebx, 1
485 heavyiron 1626
    mcall
31 halyavin 1627
    mov     cl, 13
1628
    mov     eax,63
1629
    mov     ebx, 1
485 heavyiron 1630
    mcall
31 halyavin 1631
    mov     cl, 10
1632
    mov     eax,63
1633
    mov     ebx, 1
485 heavyiron 1634
    mcall
31 halyavin 1635
    ret
1636
 
1637
dps_001:
1638
    mov     eax,63
1639
    mov     ebx, 1
1640
    push    esi
485 heavyiron 1641
    mcall
31 halyavin 1642
 
1643
    pop     esi
1644
    inc     esi
1645
    jmp     dps_000
1646
end if
1647
 
1648
 
1649
;****************************************************************************
1650
;    Function
1651
;       print_text
1652
;
1653
;   Description
1654
;       display the url (full path) text
1655
;
1656
;****************************************************************************
1657
print_text:
1658
    ; Draw a bar to blank out previous text
1659
    mov     eax,13
1660
    mov     ebx,30*65536+URLMAXLEN*6  ; 50 should really be [len], and 103 [xa]
1661
    mov     ecx,[ya]
1662
    shl     ecx,16
1663
    mov     cx,9
1664
    mov     edx,0xFFFFFF
485 heavyiron 1665
    mcall
31 halyavin 1666
 
1667
    ; write text
1668
    mov     eax,4
1669
    mov     ebx,30*65536
1670
    add     ebx,[ya]
1671
    mov     ecx,0x000000
1672
    mov     edx,[addr]
1673
    mov     esi,URLMAXLEN
485 heavyiron 1674
    mcall
31 halyavin 1675
 
1676
    ret
1677
 
1678
;   *********************************************
1679
;   *******  WINDOW DEFINITIONS AND DRAW ********
1680
;   *********************************************
1681
 
1682
draw_window:
1645 clevermous 1683
 
1684
	cmp		byte [params], 0
1685
	jz		.noret
1686
 
31 halyavin 1687
    mov     eax,12                    ; function 12:tell os about windowdraw
1688
    mov     ebx,1                     ; 1, start of draw
485 heavyiron 1689
    mcall
31 halyavin 1690
 
1645 clevermous 1691
	; это несколько загадочно, но если не рисовать окошко совсем, прога не пашет.
1692
 
1693
    mov     eax,0                     ; function 0 : define and draw window
1694
    mov     ebx,0          ; [x start] *65536 + [x size]
1695
    mov     ecx,0          ; [y start] *65536 + [y size]
1696
    mov     edx,0x14ffffff            ; color of work area RRGGBB,8->color gl
1697
    mov     edi,title                 ; WINDOW LABEL
1698
    mcall
1699
 
1700
    mov     eax,12                    ; function 12:tell os about windowdraw
1701
    mov     ebx,2                     ; 2, end of draw
1702
    mcall
1703
 
1704
	ret
1705
 
1706
.noret:
1707
    mov     eax,12                    ; function 12:tell os about windowdraw
1708
    mov     ebx,1                     ; 1, start of draw
1709
    mcall
1710
 
31 halyavin 1711
                                   ; DRAW WINDOW
1712
    mov     eax,0                     ; function 0 : define and draw window
1645 clevermous 1713
    mov     ebx,50*65536+600          ; [x start] *65536 + [x size]
1714
    mov     ecx,350*65536+200          ; [y start] *65536 + [y size]
661 ataualpa 1715
    mov     edx,0x14ffffff            ; color of work area RRGGBB,8->color gl
485 heavyiron 1716
    mov     edi,title                 ; WINDOW LABEL
1717
    mcall
31 halyavin 1718
 
661 ataualpa 1719
 
31 halyavin 1720
    mov     esi, URLMAXLEN            ; URL
1721
    mov     eax,4                     ; function 4 : write text to window
1722
    mov     ebx,30*65536+38           ; [x start] *65536 + [y start]
1723
    mov     ecx,0x000000              ; color of text RRGGBB
1724
    mov     edx,document_user         ; pointer to text beginning
485 heavyiron 1725
    mcall
31 halyavin 1726
 
1645 clevermous 1727
    ; mov     eax,38
1728
    ; mov     ebx,5*65536+545
1729
    ; mov     ecx,60*65536+60
1730
    ; mov     edx,0x000000
1731
    ; mcall
31 halyavin 1732
 
485 heavyiron 1733
    ;mov     eax,38
1734
    ;mov     ebx,5*65536+545
1645 clevermous 1735
    ; mov     ecx,[winys]
1736
    ; shl     ecx,16
1737
    ; add     ecx,[winys]
1738
    ; sub     ecx,26*65536+26
485 heavyiron 1739
    ;mov     edx,0x000000
1645 clevermous 1740
    ;mcall
31 halyavin 1741
                                   ; RELOAD
1742
    mov     eax,8                     ; function 8 : define and draw button
1743
    mov     ebx,388*65536+50          ; [x start] *65536 + [x size]
1744
    mov     ecx,34*65536+14           ; [y start] *65536 + [y size]
1745
    mov     edx,22                    ; button id
1746
    mov     esi,0x5588dd              ; button color RRGGBB
485 heavyiron 1747
    mcall
31 halyavin 1748
 
1749
                                   ; URL
485 heavyiron 1750
    ;mov     eax,8                     ; function 8 : define and draw button
31 halyavin 1751
    mov     ebx,10*65536+12          ; [x start] *65536 + [x size]
1752
    mov     ecx,34*65536+12           ; [y start] *65536 + [y size]
1753
    mov     edx,10                    ; button id
485 heavyiron 1754
    ;mov     esi,0x5588dd              ; button color RRGGBB
1755
    mcall
31 halyavin 1756
 
1757
                                   ; STOP
485 heavyiron 1758
    ;mov     eax,8                     ; function 8 : define and draw button
31 halyavin 1759
    mov     ebx,443*65536+50          ; [x start] *65536 + [x size]
1760
    mov     ecx,34*65536+14           ; [y start] *65536 + [y size]
1761
    mov     edx,24                    ; button id
485 heavyiron 1762
    ;mov     esi,0x5588dd              ; button color RRGGBB
1763
    mcall
31 halyavin 1764
 
1645 clevermous 1765
									; SAVE
1766
    mov     ebx,(443+55)*65536+50          ; [x start] *65536 + [x size]
1767
    mov     ecx,34*65536+14           ; [y start] *65536 + [y size]
1768
    mov     edx,26                    ; button id
1769
    ;mov     esi,0x5588dd              ; button color RRGGBB
1770
    mcall
1771
 
31 halyavin 1772
                                   ; BUTTON TEXT
1773
    mov     eax,4                     ; function 4 : write text to window
1774
    mov     ebx,390*65536+38          ; [x start] *65536 + [y start]
1775
    mov     ecx,0xffffff              ; color of text RRGGBB
1776
    mov     edx,button_text           ; pointer to text beginning
1645 clevermous 1777
    mov     esi,30                    ; text length
485 heavyiron 1778
    mcall
31 halyavin 1779
 
1780
    call    display_page
1781
 
1782
    mov     eax,12                    ; function 12:tell os about windowdraw
1783
    mov     ebx,2                     ; 2, end of draw
485 heavyiron 1784
    mcall
31 halyavin 1785
 
1786
    ret
1787
 
1645 clevermous 1788
align 4
1789
@IMPORT:
31 halyavin 1790
 
1645 clevermous 1791
library	libini, 'libini.obj'
1792
 
1793
import	libini, \
1794
	ini.get_str, 'ini_get_str',	\
1795
	ini.get_int, 'ini_get_int'
1796
 
1797
 
1798
fileinfo		dd	2, 0, 0
1799
final_size		dd		0
1800
final_buffer	dd		0
1801
				db	'/rd/1/.download',0
1802
 
1803
body_pos		dd		0
1804
 
1805
;fileinfo_tmp	dd	2, 0, 0
1806
buf_size		dd		0
1807
buf_ptr			dd		0
1808
				;db	'/rd/1/1',0
1809
 
1810
deba			dd		0
1811
				db		0
1812
 
1813
 
31 halyavin 1814
if DEBUGGING_STATE = DEBUGGING_ENABLED
1815
str1:       db  "Resolving...",0
1816
str3:       db  "Resolved",0
1817
str2:       db  "GotIP",0
1818
str4:       db  "GotResponse",0
1819
end if
1820
 
1645 clevermous 1821
params		db	1024 dup(0)
1822
 
1823
button_text     db      ' RELOAD    STOP       SAVE    '
31 halyavin 1824
dpx             dd      25  ; x - start of html page in pixels in window
1825
dpy             dd      65  ; for y
1826
lastletter      db      0
1827
pageyinc        dd      0
1828
display_from    dd      20
1829
pos             dd      0x0
1830
pagex           dd      0x0
1831
pagey           dd      0x0
1832
pagexs          dd      80
1833
command_on_off  dd      0x0
1834
text_type       db      1
1835
com2            dd      0x0
1836
script          dd      0x0
1837
socket          dd      0x0
1838
 
1839
addr            dd  0x0
1840
ya              dd  0x0
1841
len             dd  0x00
1842
 
1645 clevermous 1843
title         db      'Downloader',0
31 halyavin 1844
 
1845
server_ip:      db      207,44,212,20
1645 clevermous 1846
;dns_ip:         db      194,145,128,1
1847
webAddr:        times URLMAXLEN db ' '
1848
db 0
31 halyavin 1849
document_user:  db      'Click on the button to the left to enter a URL',0
1645 clevermous 1850
times URLMAXLEN+document_user-$ db 0
31 halyavin 1851
document:       db      '/'
1645 clevermous 1852
times URLMAXLEN-1       db      ' '
31 halyavin 1853
 
1645 clevermous 1854
s_contentlength		db	'Content-Length:'
1855
len_contentlength	=	15
1856
 
1857
s_chunked			db	'Transfer-Encoding: chunked'
1858
len_chunked			= $ - s_chunked
1859
 
1860
is_body			dd		0 ; 0 if headers, 1 if content
1861
is_chunked		dd		0
1862
prev_chunk_end	dd		0
1863
cur_chunk_size	dd		0
1864
 
31 halyavin 1865
string0:        db      'GET '
1866
 
1645 clevermous 1867
stringh:        db      ' HTTP/1.1',13,10,'Host: '
31 halyavin 1868
stringh_end:
1645 clevermous 1869
proxy_auth_basic: db      13,10,'Proxy-Authorization: Basic '
1870
proxy_auth_basic_end:
1871
connclose:		db		13,10,'User-Agent: Kolibrios Downloader',13,10,'Connection: Close',13,10,13,10
1872
connclose_end:
31 halyavin 1873
 
1645 clevermous 1874
base64_table	db	'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
1875
		db	'0123456789+/'
1876
 
1877
inifile		db	'/sys/network/zeroconf.ini',0
1878
 
1879
sec_proxy:
1880
key_proxy		db		'proxy',0
1881
key_proxyport		db		'port',0
1882
key_user		db		'user',0
1883
key_password		db		'password',0
1884
 
1885
request			db		256 dup(0)
1886
 
1887
proxyAddr		db		256 dup(0)
1888
proxyUser		db		256 dup(0)
1889
proxyPassword	db		256 dup(0)
1890
proxyPort		dd		80
1891
 
1892
shared_name		dd		0
1893
 
1894
;yandex:			db		'menuetos.net'
1895
;yandex_end:
1896
 
31 halyavin 1897
status          dd      0x0
1898
prev_status     dd      0x0
1899
 
1900
onoff           dd      0x0
1901
 
1902
nextupdate:     dd      0
1903
winys:          dd      400
1904
 
1645 clevermous 1905
primary_buf		rb		primary_buffer_size
1906
 
31 halyavin 1907
dnsMsgLen:      dd 0
1908
socketNum:      dd 0xFFFF
1909
dnsMsg:
1910
I_END: