?login_element?

Subversion Repositories NedoOS

Rev

Rev 1581 | 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. openstream_http
  30.  
  31. ;de=filename (without "http://"), slash always presents
  32. ;out: A!=0 => error
  33.         ex de,hl
  34. openstream_http_hl
  35.                 push hl
  36.         ld de,httphostname
  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
  46.         call strcopy ;hl->de
  47.         ld hl,80*256 ;BIG ENDIAN
  48.         ld a,(curprotocol)
  49.         cp 2
  50.         jr nz,$+4 ;1/3=http
  51.         ld h,70 ;2=gopher
  52.         ld (curport),hl
  53.         ld hl,httphostname ;pop hl
  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
  103.         push hl ;filename after ser.ver/
  104.        
  105. ;httphostname=server name (filename before slash, not including slash)
  106. ;top of stack=filename after slash (after ser.ver/)
  107.        
  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
  115. ;create socket:
  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
  122.                 LD DE,host_ia
  123.                 OS_NETCONNECT
  124.                  ld a,l ;DimkaM 12.03.2019
  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
  133. ;form GET message in DISKBUF (will be deleted in readstream)
  134.         ld de,DISKBUF
  135.  
  136.          ;ld a,0xfe
  137.          ;in a,(0xfe)
  138.          ;rra
  139.          ld a,(curprotocol)
  140. ;TODO что-то сделать с типом 3 - ничего
  141.          cp 2
  142.          jr nz,connect_nogopher ;http=1, gopher=2
  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.  
  155.         ld hl,tGET
  156.         call strcopy
  157.         dec de
  158.        
  159.          pop hl ;filename
  160.         call strcopy
  161.         dec de
  162. ;amp.dascene.net: убрать пробел в конце url:
  163.         dec de
  164.         ld a,(de)
  165.         sub ' '
  166.         jr z,$+3
  167.         inc de
  168.        
  169.         ld hl,tHTTP_host
  170.         call strcopy
  171.         dec de
  172.         ld hl,httphostname
  173.         call strcopy
  174.         dec de
  175.         ld hl,tGETend
  176.         call strcopy ;with terminator
  177.          dec de ;no terminator?
  178.          
  179. connect_makeheaderq
  180.                 ex de,hl
  181.         ld de,0xffff&(-DISKBUF)
  182.         add hl,de
  183.  
  184. ;send message to server:
  185.                 LD      a,(soc1)
  186.                 LD      DE,DISKBUF
  187. ;de=message
  188. ;hl=message size      
  189.         ;jr $
  190.                 OS_WIZNETWRITE
  191.                 bit 7,h
  192.                 jr nz,createsoc_err
  193.        
  194.         ld a,1
  195.         ld (http_firstreadflag),a
  196.        
  197.         xor a ;OK
  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.  
  214. tlocation
  215.         db "Location: ",0
  216.  
  217. readstream_http
  218.          ld (readstream_http_requestedsize),hl
  219.         add hl,de
  220.         push de ;начало буфера
  221.        
  222. http_firstreadflag=$+1
  223.         ld a,1
  224.         dec a
  225.         jp nz,readstream_http_nofirstread
  226. curprotocol=$+1
  227.          ld a,0;(curprotocol)
  228.          cp 2
  229.          jp z,readstream_http_nohead ;2=gopher
  230.          ;ld a,0xfe
  231.          ;in a,(0xfe)
  232.          ;rra
  233.          ;jr nc,readstream_http_nohead ;Caps Shift - show headers
  234.        
  235. ;read until cr,lf,cr,lf or EOF or endofbuf
  236.         push hl
  237.         push de
  238.         or a
  239.         sbc hl,de ;размер
  240.         jr readstream_http_headlines0
  241. readstream_http_headretry
  242.         push de
  243.         push hl
  244.         call yieldgetkeynolang
  245.         pop hl
  246.         pop de
  247.         cp key_esc
  248.         jp z,readstream_err
  249.  
  250. readstream_http_headlines0
  251.          ld (readstream_http_headlineaddr),de
  252. readstream_http_head0
  253.         push de
  254.         push hl ;размер
  255.         ld hl,1
  256.         ld a,(soc1)
  257.         OS_WIZNETREAD
  258.         bit 7,h
  259.         pop hl
  260.         pop de
  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
  266.         dec hl ;размер
  267.         ld a,h
  268.         or l
  269.         jp z,readstream_err
  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
  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.        
  289. ;если строка пустая, то readstream_http_headq
  290. readstream_http_headlineaddr=$+1
  291.         ld a,(0) ;ok
  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
  301. ;найти строку Location: <url> (если moved temporarily)
  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
  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.                
  322.         call closestream_http
  323.                
  324.         ld hl,curfulllink
  325.                
  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
  330.         call openstream_http_hl
  331.        
  332.         pop de
  333.         pop hl
  334.        
  335.         pop de ;начало буфера
  336. readstream_http_requestedsize=$+1
  337.         ld hl,0
  338.         ;jr $
  339.          ld a,(curprotocol)
  340.          cp 3 ;https
  341.          jr z,readstream_http_redirectq ;костыль
  342.         xor a
  343.         ld (http_firstreadflag),a ;почему-то страничка, куда переадресует amp.dascene.net, отдаёт файл без http заголовка (HTTP/1.1 200 OK...)! а WoS с заголовком!
  344. readstream_http_redirectq
  345.         jp readstream_http
  346.        
  347.        
  348. readstream_http_headq
  349.         pop de
  350.         pop hl
  351. ;TODO переделать: читать как обычно, потом искать заголовок, отрезать его, сдвинуть остаток в начало буфера и прочитать ещё столько же
  352.  
  353. readstream_http_nohead
  354.         xor a
  355.         ld (http_firstreadflag),a
  356.        
  357. readstream_http_nofirstread
  358.  
  359. readstream_loop
  360.         push hl ;докуда читать
  361.         push de ;текущий ptr
  362.         or a
  363.         sbc hl,de ;размер
  364.         jr z,readstream_err
  365.         LD      a,(soc1)
  366.         OS_WIZNETREAD
  367.         bit 7,h
  368.         jr z,readstream_loop_ok
  369.         cp ERR_EAGAIN
  370.         jr nz,readstream_err
  371.         ld hl,0
  372. readstream_loop_ok
  373.         pop de ;de=куда читали, hl=сколько прочитали
  374.         add hl,de
  375.         ex de,hl ;de=текущий ptr
  376.         pop hl ;докуда читать
  377.         jr readstream_loop
  378. readstream_err:
  379.         pop hl
  380.         pop de
  381.        
  382.         pop de ;начало буфера
  383.         or a
  384.         sbc hl,de
  385.         ret
  386.        
  387.                
  388. closestream_http
  389.                 ld e,0
  390.                 ld a,(soc1)
  391.                 OS_NETSHUTDOWN
  392.         ret
  393.  
  394. curport=$+1
  395. host_ia:
  396.         defb 0,0,80,8,8,8,8
  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
  409.         db " HTTP/1.0\r\n" ;1.0 DimkaM for nedopc.com
  410.         db "Connection: close\r\n"
  411.         db "Host: ",0
  412. tGETend
  413.         db "\r\n"
  414.         db "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n"
  415.         db "\r\n",0
  416.  
  417. ;httpgetstr
  418.         ;defb 'GET /cspr/index.htm HTTP/1.1',13,10
  419.         ;defb 'Host: dimkam.ru',13,10
  420.         ;defb 13,10
  421. dns_resolver:           ;DE-domain name
  422.     ld a,25;3
  423.     ld (dns_err_count),a
  424. dns_err_loop
  425.         ;push de
  426.         ld hl,dns_head
  427.         ld de,dnsbuf
  428.         ld bc,6
  429.         ldir
  430.         ex de,hl
  431.         ld de,dnsbuf+7
  432.         ld (hl),b;0
  433.         ld  c,256-7
  434.         ldir
  435.         ld de,dnsbuf+12
  436.         ld h,d
  437.         ld l,e
  438.         ld bc, httphostname ;pop bc
  439. name_loop:
  440.         inc hl
  441.         ld a,(bc)
  442.         ld (hl),a
  443.         inc bc
  444.         cp '.'
  445.         jr z,is_dot
  446.         or a
  447.         jr nz,name_loop
  448. is_dot:
  449.         sbc hl,de
  450.         ex de,hl
  451.         dec e
  452.         ld (hl),e
  453.         inc e
  454.         add hl,de
  455.         ld d,h
  456.         ld e,l
  457.         or a
  458.         jr nz,name_loop
  459.         inc a
  460.         inc hl
  461.         inc hl
  462.         ld (hl),a
  463.         inc hl
  464.         inc hl
  465.         ld (hl),a
  466.         inc hl
  467.         push hl
  468. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  469.         ld a, (dns_ia2)
  470.         cp 0
  471.         jp nz, skipgetdns
  472.         ld de, dns_ia2;DE= ptr to DNS buffer(4 bytes)
  473.         OS_GETDNS
  474. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;  
  475. skipgetdns:
  476.         ld de,0x0203
  477.         OS_NETSOCKET
  478.         ld a,l
  479.         ld (soc1),a
  480.         or a
  481.         jp m,dns_exiterr
  482.         ;LD     DE,dns_ia
  483.         ;OS_NETCONNECT
  484.     ;ld a,l
  485.         ;or a
  486.         ;jp m,dns_exiterr
  487.        
  488.         pop hl
  489.         push hl
  490.         ld de,0xffff&(-dnsbuf)
  491.         add hl,de
  492.         LD      a,(soc1)
  493.         LD      IX,dnsbuf
  494.         LD      DE,dns_ia
  495.         OS_WIZNETWRITE
  496.         bit 7,h
  497.         jr nz,dns_exitcode
  498. dns_err_count=$+1
  499.         ld b,25
  500.         jr recv_wait1
  501. recv_wait:
  502.         ;YIELD
  503.         push bc
  504.         call yieldgetkeynolang
  505.         pop bc
  506. recv_wait1:
  507.         push bc
  508.         ld hl,256
  509.         LD      a,(soc1)
  510.         LD      DE,dnsbuf
  511.         LD      IX,dnsbuf
  512.         OS_WIZNETREAD
  513.         pop bc
  514.         ;ld a,h
  515.         ;or l
  516.         bit 7,h
  517.         jr z,recv_wait_end
  518.         djnz recv_wait
  519.         ;ld a,54        ;ERR_CONNRESET
  520.         ;ld (errno),a
  521.         jr dns_exiterr
  522. recv_wait_end:
  523.         ;bit 7,h
  524.         ;jr nz,dns_exitcode
  525.         ;ld a,65                ;ERR_HOSTUNREACH
  526.         ;ld (errno),a
  527.         ld a,(dnsbuf+3)
  528.         and 0x0f       
  529.         jr nz,dns_exiterr
  530. dns_exitcode:
  531.         LD      a,(soc1)
  532.         LD      E,0
  533.         OS_NETSHUTDOWN
  534.         pop hl
  535. reqpars_l
  536.         inc hl
  537.         inc hl
  538.         inc hl
  539.         ld a,(hl)
  540.         ld de,7
  541.         add hl,de
  542.         ld b,(hl)
  543.         inc hl
  544.         ld c,(hl)
  545.         inc hl
  546.         dec a
  547.         ret z
  548.         cp 4
  549.         jr nz,exiterr1
  550.         add hl,bc
  551.         jr reqpars_l
  552. dns_exiterr:
  553.         pop af
  554.         LD      a,(soc1)
  555.         LD      E,0
  556.         OS_NETSHUTDOWN
  557.     ld a,(dns_err_count)
  558.     add a,a
  559.     ld (dns_err_count),a
  560.     jp nc,dns_err_loop
  561. exiterr1:
  562.     ld hl,0
  563.         ret
  564. dns_head
  565.         defb 0x11,0x22,0x01,0x00,0x00,0x01
  566.  
  567. ;struct sockaddr_in {unsigned char sin_family;unsigned short sin_port;
  568. ;       struct in_addr sin_addr;char sin_zero[8];};
  569. dns_ia:
  570.         defb 0
  571.         db 0,53 ;port (big endian)
  572. dns_ia2:
  573.         db 0,0,0,0 ;ip (big endian)
  574.