?login_element?

Subversion Repositories NedoOS

Rev

Rev 1579 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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