Subversion Repositories NedoOS

Rev

Rev 1656 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
33 Alone 1
httphostname=DISKBUF+0x400
2
dnsbuf=DISKBUF+0x0600
3
 
28 Alone 4
IPPROTO_TCP EQU 6
5
IPPROTO_UDP EQU 17
6
 
7
AF_UNSPEC EQU 0
8
AF_INET EQU 2
9
AF_INET6 EQU 23
10
 
11
SOCK_STREAM EQU 0x01    ;tcp/ip
12
SOCK_DGRAM      EQU 0x03                ;udp/ip
13
 
14
SHUT_RDWR               EQU 2
15
ERR_INTR                EQU 4
16
ERR_NFILE               EQU 23
519 dimkam 17
ERR_EAGAIN              EQU 35          ;/* Try again */
28 Alone 18
ERR_ALREADY     EQU 37
19
ERR_NOTSOCK     EQU 38
20
ERR_EMSGSIZE    EQU 40    ;/* Message too long */
21
ERR_PROTOTYPE   EQU 41
22
ERR_AFNOSUPPORT EQU 47
23
ERR_HOSTUNREACH EQU 65
24
ERR_CONNRESET   EQU 54
25
ERR_NOTCONN     EQU 57
26
 
304 dimkam 27
proxserv
1003 dimkam 28
        db "nedoos.ru/p/",0
28 Alone 29
openstream_http
1656 dimkam 30
 
32 Alone 31
;de=filename (without "http://"), slash always presents
32
;out: A!=0 => error
33
        ex de,hl
68 Alone 34
openstream_http_hl
304 dimkam 35
                push hl
32 Alone 36
        ld de,httphostname
304 dimkam 37
                ld a,(curprotocol)
38
                cp 3
39
                jr nz,nonhttps
40
                ld hl,proxserv
41
                call strcopy
42
                dec de
43
nonhttps
44
                pop hl
45
        ;push de
444 alone 46
        call strcopy ;hl->de
249 alone 47
        ld hl,80*256 ;BIG ENDIAN
261 alone 48
        ld a,(curprotocol)
303 alone 49
        cp 2
50
        jr nz,$+4 ;1/3=http
261 alone 51
        ld h,70 ;2=gopher
249 alone 52
        ld (curport),hl
304 dimkam 53
        ld hl,httphostname ;pop hl
249 alone 54
        ;call findslash
55
openstream_http_findslash0
56
         ld a,(hl)
57
         cp ':'
58
         jr z,openstream_http_setport
59
         cp '/'
60
         jr z,openstream_http_slash
61
         or a
62
         jr z,openstream_http_slash
63
         inc hl
64
         jr openstream_http_findslash0
65
openstream_http_setport
66
        ld (hl),0 ;end of httphostname
67
;decode port
68
        ld de,0 ;oldport
69
openstream_http_decodeport0
70
        inc hl
71
        ld a,(hl)
72
        sub '0'
73
        cp 10
74
        jr nc,openstream_http_decodeportq
75
        push hl
76
        ld h,d
77
        ld l,e
78
        add hl,hl
79
        add hl,hl
80
        add hl,de
81
        add hl,hl ;hl=oldport*10
82
        add a,l
83
        ld e,a
84
        adc a,h
85
        sub e
86
        ld d,a ;de=port
87
        pop hl
88
        jr openstream_http_decodeport0
89
openstream_http_decodeportq
90
;de=port
91
        ld a,d
92
        ld d,e
93
        ld e,a
94
        ld (curport),de ;BIG ENDIAN
95
;hl=delimiter after ser.ver:NNN
96
openstream_http_slash
97
;hl=at delimiter after hostname and maybe port
98
        xor a
99
        cp (hl)
100
        ld (hl),a ;end of httphostname
101
        jr z,$+3
102
        inc hl ;after delimiter
32 Alone 103
        push hl ;filename after ser.ver/
104
 
105
;httphostname=server name (filename before slash, not including slash)
33 Alone 106
;top of stack=filename after slash (after ser.ver/)
32 Alone 107
 
30 DimkaM 108
                call dns_resolver
109
                ld a,l
110
                or h
111
                jp z,CONNECTIONERROR
112
                ld de,host_ia+3
113
                ld bc,4
114
                ldir
28 Alone 115
;create socket:
30 DimkaM 116
                ld de,SOCK_STREAM+(AF_INET<<8)
117
                OS_NETSOCKET
118
                ld a,l
119
                or a
120
                jp m,CONNECTIONERROR;?C_EXIT
121
                ld (soc1),a
32 Alone 122
                LD DE,host_ia
30 DimkaM 123
                OS_NETCONNECT
32 Alone 124
                 ld a,l ;DimkaM 12.03.2019
30 DimkaM 125
                or a
126
                jp p,connect_ok
127
createsoc_err
128
                ld a,(soc1)
129
                ld e,0
130
                OS_NETSHUTDOWN
131
                jp CONNECTIONERROR
132
connect_ok
28 Alone 133
;form GET message in DISKBUF (will be deleted in readstream)
251 alone 134
        ld de,DISKBUF
135
 
261 alone 136
         ;ld a,0xfe
137
         ;in a,(0xfe)
138
         ;rra
139
         ld a,(curprotocol)
344 alone 140
;TODO - ᤥ  ⨯ 3 - 祣
304 dimkam 141
         cp 2
142
         jr nz,connect_nogopher ;http=1, gopher=2
251 alone 143
         pop hl ;filename
144
         call strcopy
145
         dec de
146
         ld a,0xd
147
         ld (de),a
148
         inc de
149
         ld a,0xa
150
         ld (de),a
151
         inc de
152
         jr connect_makeheaderq
153
connect_nogopher
154
 
28 Alone 155
        ld hl,tGET
156
        call strcopy
30 DimkaM 157
        dec de
32 Alone 158
 
28 Alone 159
         pop hl ;filename
160
        call strcopy
30 DimkaM 161
        dec de
344 alone 162
;amp.dascene.net:  ஡   url:
68 Alone 163
        dec de
164
        ld a,(de)
165
        sub ' '
166
        jr z,$+3
167
        inc de
168
 
28 Alone 169
        ld hl,tHTTP_host
170
        call strcopy
30 DimkaM 171
        dec de
28 Alone 172
        ld hl,httphostname
173
        call strcopy
30 DimkaM 174
        dec de
28 Alone 175
        ld hl,tGETend
32 Alone 176
        call strcopy ;with terminator
177
         dec de ;no terminator?
251 alone 178
 
179
connect_makeheaderq
30 DimkaM 180
                ex de,hl
181
        ld de,0xffff&(-DISKBUF)
182
        add hl,de
33 Alone 183
 
31 Alone 184
;send message to server:
30 DimkaM 185
                LD      a,(soc1)
186
                LD      DE,DISKBUF
31 Alone 187
;de=message
188
;hl=message size       
33 Alone 189
        ;jr $
30 DimkaM 190
                OS_WIZNETWRITE
191
                bit 7,h
192
                jr nz,createsoc_err
28 Alone 193
 
31 Alone 194
        ld a,1
195
        ld (http_firstreadflag),a
196
 
197
        xor a ;OK
28 Alone 198
        ret
199
 
200
;hl = poi to filename in string
201
;out: de = after last dot or start
202
findlastdot
203
        ld d,h
204
        ld e,l ;de = after last dot
205
findlastdot0
206
        ld a,[hl]
207
        inc hl
208
        or a
209
        ret z
210
        cp '.'
211
        jr nz,findlastdot0
212
        jr findlastdot
213
 
68 Alone 214
tlocation
215
        db "Location: ",0
28 Alone 216
 
217
readstream_http
68 Alone 218
         ld (readstream_http_requestedsize),hl
30 DimkaM 219
        add hl,de
344 alone 220
        push de ;砫 
31 Alone 221
 
222
http_firstreadflag=$+1
223
        ld a,1
224
        dec a
261 alone 225
        jp nz,readstream_http_nofirstread
226
curprotocol=$+1
227
         ld a,0;(curprotocol)
303 alone 228
         cp 2
444 alone 229
         jp z,readstream_http_nohead ;2=gopher
296 alone 230
         ;ld a,0xfe
231
         ;in a,(0xfe)
232
         ;rra
233
         ;jr nc,readstream_http_nohead ;Caps Shift - show headers
251 alone 234
 
31 Alone 235
;read until cr,lf,cr,lf or EOF or endofbuf
236
        push hl
237
        push de
238
        or a
344 alone 239
        sbc hl,de ;ࠧ
68 Alone 240
        jr readstream_http_headlines0
32 Alone 241
readstream_http_headretry
242
        push de
243
        push hl
244
        call yieldgetkeynolang
245
        pop hl
246
        pop de
116 alone 247
        cp key_esc
73 Alone 248
        jp z,readstream_err
2202 dimkam 249
        display "readstream_http_head0 ", readstream_http_head0
68 Alone 250
readstream_http_headlines0
251
         ld (readstream_http_headlineaddr),de
31 Alone 252
readstream_http_head0
253
        push de
344 alone 254
        push hl ;ࠧ
31 Alone 255
        ld hl,1
256
        ld a,(soc1)
257
        OS_WIZNETREAD
258
        bit 7,h
259
        pop hl
260
        pop de
519 dimkam 261
        jr z,readstream_http_head_ok
262
        cp ERR_EAGAIN
263
    jr z,readstream_http_head0 ; ⢥  ᯥ ਩
264
        jp readstream_err
265
readstream_http_head_ok
344 alone 266
        dec hl ;ࠧ
30 DimkaM 267
        ld a,h
268
        or l
1656 dimkam 269
        jp z,readstream_err
31 Alone 270
        ld b,d
271
        ld c,e
272
        inc de
273
        ld a,(bc)
274
        cp 0x0a
275
        jr nz,readstream_http_head0
276
        dec bc
277
        ld a,(bc)
278
        cp 0x0d
279
        jr nz,readstream_http_head0
68 Alone 280
        ;dec bc
281
        ;ld a,(bc)
282
        ;cp 0x0a
283
        ;jr nz,readstream_http_head0
284
        ;dec bc
285
        ;ld a,(bc)
286
        ;cp 0x0d
287
        ;jr nz,readstream_http_head0
288
 
344 alone 289
;᫨ ப ,  readstream_http_headq
68 Alone 290
readstream_http_headlineaddr=$+1
555 alone 291
        ld a,(0) ;ok
68 Alone 292
        cp 0x0d
293
        jr z,readstream_http_headq
294
 
295
        push de
296
        push hl
297
 
298
        ;jr $
299
        ld hl,(readstream_http_headlineaddr)
300
        ld de,tlocation
344 alone 301
; ப Location: <url> (᫨ moved temporarily)
68 Alone 302
        call strcp_tillde0
303
        ld b,h
304
        ld c,l
305
        pop hl
306
        pop de
307
        jr nz,readstream_http_headlines0
308
;bc=url
1656 dimkam 309
        ;push bc
310
                ;஢  ⥪騩 
311
                ld hl,curfulllink - 1
312
.strcpy
313
                inc hl
314
                ld a,(bc)
315
                ld (hl),a
316
                inc bc
317
                cp 0x0d
318
                jr nz,.strcpy
319
                xor a
320
                ld (hl),a
321
 
68 Alone 322
        call closestream_http
1656 dimkam 323
 
324
        ld hl,curfulllink
325
 
444 alone 326
         call isprotocolpresent ;out: nz=protocol absent (hl=link), z=protocol present (a=protocol (0=file, 1=http), hl=after "//")
327
         jr nz,readstream_http_redirect_noprotocol
328
         ld (curprotocol),a
329
readstream_http_redirect_noprotocol
68 Alone 330
        call openstream_http_hl
331
 
31 Alone 332
        pop de
333
        pop hl
68 Alone 334
 
344 alone 335
        pop de ;砫 
68 Alone 336
readstream_http_requestedsize=$+1
337
        ld hl,0
338
        ;jr $
444 alone 339
         ld a,(curprotocol)
340
         cp 3 ;https
341
         jr z,readstream_http_redirectq ;
68 Alone 342
        xor a
444 alone 343
        ld (http_firstreadflag),a ;祬- ࠭窠, 㤠 ॠ amp.dascene.net, ⤠ 䠩  http  (HTTP/1.1 200 OK...)!  WoS  !
344
readstream_http_redirectq
68 Alone 345
        jp readstream_http
346
 
347
 
348
readstream_http_headq
349
        pop de
350
        pop hl
344 alone 351
;TODO ।:   筮, ⮬ ᪠ , १ , ᤢ ⮪  砫     ⮫쪮 
68 Alone 352
 
251 alone 353
readstream_http_nohead
68 Alone 354
        xor a
355
        ld (http_firstreadflag),a
31 Alone 356
 
357
readstream_http_nofirstread
358
 
359
readstream_loop
344 alone 360
        push hl ;㤠 
361
        push de ;⥪騩 ptr
31 Alone 362
        or a
344 alone 363
        sbc hl,de ;ࠧ
31 Alone 364
        jr z,readstream_err
30 DimkaM 365
        LD      a,(soc1)
366
        OS_WIZNETREAD
367
        bit 7,h
519 dimkam 368
        jr z,readstream_loop_ok
369
        cp ERR_EAGAIN
30 DimkaM 370
        jr nz,readstream_err
519 dimkam 371
        ld hl,0
372
readstream_loop_ok
344 alone 373
        pop de ;de=㤠 ⠫, hl=᪮쪮 ⠫
30 DimkaM 374
        add hl,de
344 alone 375
        ex de,hl ;de=⥪騩 ptr
376
        pop hl ;㤠 
30 DimkaM 377
        jr readstream_loop
378
readstream_err:
379
        pop hl
380
        pop de
31 Alone 381
 
344 alone 382
        pop de ;砫 
30 DimkaM 383
        or a
384
        sbc hl,de
385
        ret
386
 
387
 
28 Alone 388
closestream_http
30 DimkaM 389
                ld e,0
390
                ld a,(soc1)
391
                OS_NETSHUTDOWN
28 Alone 392
        ret
393
 
249 alone 394
curport=$+1
30 DimkaM 395
host_ia:
396
        defb 0,0,80,8,8,8,8
28 Alone 397
;httpslashcurdir
398
        db '/'
399
httpcurdir
400
;server/path (without / in the end)
401
        ds 128
402
 
403
soc1
404
        db 0 ;current socket
405
 
406
tGET
407
        db "GET /",0
408
tHTTP_host
31 Alone 409
        db " HTTP/1.0\r\n" ;1.0 DimkaM for nedopc.com
68 Alone 410
        db "Connection: close\r\n"
28 Alone 411
        db "Host: ",0
412
tGETend
68 Alone 413
        db "\r\n"
973 dimkam 414
        db "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n"
68 Alone 415
        db "\r\n",0
28 Alone 416
 
417
;httpgetstr
418
        ;defb 'GET /cspr/index.htm HTTP/1.1',13,10
419
        ;defb 'Host: dimkam.ru',13,10
2202 dimkam 420
        ;defb 13,10     
421
        display "dns_resolver ", $
30 DimkaM 422
dns_resolver:           ;DE-domain name
2202 dimkam 423
        ld de,httphostname-1
424
        ld hl,dnsbuf-1
425
dns_ip_loop1
426
        inc hl
427
        ld (hl),0
428
dns_ip_loop
429
        inc de
430
        ld a,(de)
431
        or 0
432
        jr nz,dns_ip_not_zero
433
        ld hl,dnsbuf
434
        ret
435
 
436
dns_ip_not_zero
437
        cp '.'
438
        jr z,dns_ip_loop1
439
 
440
        sub '0'
441
        jr c,dns_not_ip
442
        cp 9+1
443
        jr nc,dns_not_ip
444
        ld c,a
445
        ld a,(hl)
446
        add a
447
        ld b,a
448
        add a
449
        add a
450
        add b
451
        add c
452
        ld (hl),a
453
        jr dns_ip_loop
454
 
455
dns_not_ip
33 Alone 456
    ld a,25;3
457
    ld (dns_err_count),a
458
dns_err_loop
30 DimkaM 459
        ;push de
460
        ld hl,dns_head
461
        ld de,dnsbuf
462
        ld bc,6
463
        ldir
464
        ex de,hl
465
        ld de,dnsbuf+7
33 Alone 466
        ld (hl),b;0
467
        ld  c,256-7
30 DimkaM 468
        ldir
469
        ld de,dnsbuf+12
470
        ld h,d
471
        ld l,e
472
        ld bc, httphostname ;pop bc
473
name_loop:
474
        inc hl
475
        ld a,(bc)
476
        ld (hl),a
477
        inc bc
478
        cp '.'
479
        jr z,is_dot
480
        or a
481
        jr nz,name_loop
482
is_dot:
483
        sbc hl,de
484
        ex de,hl
485
        dec e
486
        ld (hl),e
487
        inc e
488
        add hl,de
489
        ld d,h
490
        ld e,l
491
        or a
492
        jr nz,name_loop
493
        inc a
494
        inc hl
495
        inc hl
496
        ld (hl),a
497
        inc hl
498
        inc hl
499
        ld (hl),a
500
        inc hl
501
        push hl
1579 kulich 502
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1581 kulich 503
        ld a, (dns_ia2)
504
        cp 0
505
        jp nz, skipgetdns
1579 kulich 506
        ld de, dns_ia2;DE= ptr to DNS buffer(4 bytes)
507
        OS_GETDNS
2202 dimkam 508
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1581 kulich 509
skipgetdns:
30 DimkaM 510
        ld de,0x0203
511
        OS_NETSOCKET
512
        ld a,l
513
        ld (soc1),a
514
        or a
515
        jp m,dns_exiterr
409 dimkam 516
        ;LD     DE,dns_ia
517
        ;OS_NETCONNECT
518
    ;ld a,l
519
        ;or a
520
        ;jp m,dns_exiterr
30 DimkaM 521
 
522
        pop hl
523
        push hl
524
        ld de,0xffff&(-dnsbuf)
525
        add hl,de
526
        LD      a,(soc1)
409 dimkam 527
        LD      IX,dnsbuf
528
        LD      DE,dns_ia
30 DimkaM 529
        OS_WIZNETWRITE
530
        bit 7,h
531
        jr nz,dns_exitcode
33 Alone 532
dns_err_count=$+1
30 DimkaM 533
        ld b,25
534
        jr recv_wait1
535
recv_wait:
32 Alone 536
        ;YIELD
537
        push bc
538
        call yieldgetkeynolang
539
        pop bc
30 DimkaM 540
recv_wait1:
541
        push bc
542
        ld hl,256
543
        LD      a,(soc1)
544
        LD      DE,dnsbuf
409 dimkam 545
        LD      IX,dnsbuf
30 DimkaM 546
        OS_WIZNETREAD
547
        pop bc
409 dimkam 548
        ;ld a,h
549
        ;or l
550
        bit 7,h
551
        jr z,recv_wait_end
30 DimkaM 552
        djnz recv_wait
32 Alone 553
        ;ld a,54        ;ERR_CONNRESET
30 DimkaM 554
        ;ld (errno),a
555
        jr dns_exiterr
556
recv_wait_end:
513 dimkam 557
        ;bit 7,h
558
        ;jr nz,dns_exitcode
32 Alone 559
        ;ld a,65                ;ERR_HOSTUNREACH
30 DimkaM 560
        ;ld (errno),a
561
        ld a,(dnsbuf+3)
562
        and 0x0f       
563
        jr nz,dns_exiterr
564
dns_exitcode:
565
        LD      a,(soc1)
566
        LD      E,0
567
        OS_NETSHUTDOWN
568
        pop hl
58 DimkaM 569
reqpars_l
570
        inc hl
571
        inc hl
572
        inc hl
573
        ld a,(hl)
574
        ld de,7
30 DimkaM 575
        add hl,de
58 DimkaM 576
        ld b,(hl)
577
        inc hl
578
        ld c,(hl)
579
        inc hl
580
        dec a
581
        ret z
582
        cp 4
583
        jr nz,exiterr1
584
        add hl,bc
585
        jr reqpars_l
30 DimkaM 586
dns_exiterr:
587
        pop af
588
        LD      a,(soc1)
589
        LD      E,0
590
        OS_NETSHUTDOWN
33 Alone 591
    ld a,(dns_err_count)
58 DimkaM 592
    add a,a
33 Alone 593
    ld (dns_err_count),a
594
    jp nc,dns_err_loop
58 DimkaM 595
exiterr1:
596
    ld hl,0
30 DimkaM 597
        ret
598
dns_head
599
        defb 0x11,0x22,0x01,0x00,0x00,0x01
331 alone 600
 
601
;struct sockaddr_in {unsigned char sin_family;unsigned short sin_port;
602
;       struct in_addr sin_addr;char sin_zero[8];};
30 DimkaM 603
dns_ia:
235 alone 604
        defb 0
605
        db 0,53 ;port (big endian)
1579 kulich 606
dns_ia2:
1581 kulich 607
        db 0,0,0,0 ;ip (big endian)