Subversion Repositories Kolibri OS

Rev

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

  1. #ifndef INCLUDE_LIBHTTP_H
  2. #define INCLUDE_LIBHTTP_H
  3.  
  4. #ifndef INCLUDE_KOLIBRI_H
  5. #include "../lib/kolibri.h"
  6. #endif
  7.  
  8. #ifndef INCLUDE_DLL_H
  9. #include "../lib/dll.h"
  10. #endif
  11.  
  12. dword libHTTP = #alibHTTP;
  13. char alibHTTP[] = "/sys/lib/http.obj";
  14.  
  15. dword http_lib_init          = #aHTTPinit;
  16. dword http_get               = #aHTTPget;
  17. dword http_head              = #aHTTPhead;
  18. dword http_post              = #aHTTPpost;
  19. dword http_find_header_field = #aFHF;
  20. dword http_send              = #aHTTPsend;
  21. dword http_receive           = #aHTTPreceive;
  22. dword http_disconnect        = #aHTTPdisconnect;
  23. dword http_free              = #aHTTPfree;
  24. dword uri_escape             = #aURIescape;
  25. dword uri_unescape           = #aURIunescape;
  26. $DD 2 dup 0
  27.  
  28. char aHTTPinit[]             = "lib_init";
  29. char aHTTPget[]              = "get";
  30. char aHTTPhead[]             = "head";
  31. char aHTTPpost[]             = "post";
  32. char aFHF[]                  = "find_header_field";
  33. char aHTTPsend[]             = "send";
  34. char aHTTPreceive[]          = "receive";
  35. char aHTTPdisconnect[]       = "disconnect";
  36. char aHTTPfree[]             = "free";
  37. char aURIescape[]            = "escape";
  38. char aURIunescape[]          = "unescape";
  39.  
  40. // status flags
  41. #define FLAG_HTTP11             1 << 0
  42. #define FLAG_GOT_HEADER         1 << 1
  43. #define FLAG_GOT_ALL_DATA       1 << 2
  44. #define FLAG_CONTENT_LENGTH     1 << 3
  45. #define FLAG_CHUNKED            1 << 4
  46. #define FLAG_CONNECTED          1 << 5
  47.  
  48. // user flags
  49. #define FLAG_KEEPALIVE          1 << 8
  50. #define FLAG_MULTIBUFF          1 << 9
  51.  
  52. // error flags
  53. #define FLAG_INVALID_HEADER     1 << 16
  54. #define FLAG_NO_RAM             1 << 17
  55. #define FLAG_SOCKET_ERROR       1 << 18
  56. #define FLAG_TIMEOUT_ERROR      1 << 19
  57. #define FLAG_TRANSFER_FAILED    1 << 20
  58.  
  59. struct  http_msg{
  60.         dword   socket;
  61.         dword   flags;
  62.         dword   write_ptr;
  63.         dword   buffer_length;
  64.         dword   chunk_ptr;
  65.         dword   timestamp;
  66.         dword   status;
  67.         dword   header_length;
  68.         dword   content_ptr;
  69.         dword   content_length;
  70.         dword   content_received;
  71.         char    http_header;
  72. };
  73.  
  74.  
  75. #define URL_SIZE 4000
  76. //===================================================//
  77. //                                                   //
  78. //                       HTTP                        //
  79. //                                                   //
  80. //===================================================//
  81.  
  82. struct _http
  83. {
  84.         dword cur_url;
  85.         dword transfer;
  86.         dword content_length;
  87.         dword content_received;
  88.         dword status_code;
  89.         dword receive_result;
  90.         dword content_pointer;
  91.  
  92.         dword get();
  93.         bool stop();
  94.         void hfree();
  95.         void receive();
  96.         dword header_field();
  97. };
  98.  
  99. dword _http::get(dword _url)
  100. {
  101.         cur_url = _url;
  102.         http_get stdcall (_url, 0, 0, #accept_language);
  103.         transfer = EAX;
  104.         return transfer;
  105. }
  106.  
  107. void _http::hfree()
  108. {
  109.         http_free stdcall (transfer);
  110.         transfer=0;
  111. }
  112.  
  113. bool _http::stop()
  114. {
  115.         if (transfer)
  116.         {
  117.                 EAX = transfer;
  118.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  119.                 $push   EAX                                                     // save it on the stack
  120.                 http_free stdcall (transfer);   // abort connection
  121.                 $pop    EAX                                                    
  122.                 free(EAX);                                              // free data
  123.                 transfer=0;
  124.                 return true;    
  125.         }
  126.         return false;
  127. }
  128.  
  129. void _http::receive()
  130. {
  131.         http_receive stdcall (transfer);
  132.         receive_result = EAX;
  133.  
  134.         EDI = transfer;
  135.         if (!EAX) {
  136.                 status_code = EDI.http_msg.status;
  137.                 content_pointer = EDI.http_msg.content_ptr;
  138.         }
  139.         content_length = EDI.http_msg.content_length;
  140.         content_received = EDI.http_msg.content_received;
  141. }
  142.  
  143. :dword _http::header_field(dword _header_field, _dst, _size)
  144. {
  145.         http_find_header_field stdcall (transfer, _header_field);
  146.         if (EAX!=0) {
  147.                 ESI = EAX;
  148.                 EDI = _dst;
  149.                 EDX = _size;
  150.                 do {
  151.                         $lodsb;
  152.                         $stosb;
  153.                         $dec edx
  154.                 } while (AL != 0) && (AL != 13) && (AL != 10) && (EDX>0);
  155.                 DSBYTE[EDI-1]='\0';
  156.                 debugln(_dst);
  157.                 return _dst;
  158.         }
  159.         return NULL;
  160. }
  161.  
  162.  
  163. //===================================================//
  164. //                                                   //
  165. //                 CHECK PATH TYPE                   //
  166. //                                                   //
  167. //===================================================//
  168.  
  169. :int check_is_the_adress_local(dword _in)
  170. {
  171.         if (ESBYTE[_in]!='/') return false;
  172.         _in++;
  173.         if(!strncmp(_in,"rd/",3)) return true;
  174.         if(!strncmp(_in,"fd/",3)) return true;
  175.         if(!strncmp(_in,"hd",2)) return true;
  176.         if(!strncmp(_in,"bd",2)) return true;
  177.         if(!strncmp(_in,"cd",2)) return true;
  178.         if(!strncmp(_in,"sys/",4)) return true;
  179.         if(!strncmp(_in,"tmp",3)) return true;
  180.         if(!strncmp(_in,"usbhd",5)) return true;
  181.         if(!strncmp(_in,"kolibrios",9)) return true;
  182.         return false;
  183. }
  184.  
  185. :int check_is_the_url_absolute(dword _in)
  186. {
  187.         if(!strncmp(_in,"ftp:",4)) return true;
  188.         if(!strncmp(_in,"http:",5)) return true;
  189.         if(!strncmp(_in,"https:",6)) return true;
  190.         if(!strncmp(_in,"mailto:",7)) return true;
  191.         if(!strncmp(_in,"tel:",4)) return true;
  192.         if(!strncmp(_in,"#",1)) return true;
  193.         if(!strncmp(_in,"WebView:",8)) return true;
  194.         if(check_is_the_adress_local(_in)) return true;
  195.         return false;
  196. }
  197.  
  198. :dword get_absolute_url(dword new_URL, base_URL)
  199. {
  200.         int i;
  201.         dword orig_URL = new_URL;
  202.         char newurl[URL_SIZE+1];
  203.         strcpy(#newurl, base_URL);
  204.  
  205.         while (i=strstr(new_URL, "&amp;")) strcpy(i+1, i+5);
  206.  
  207.         if (check_is_the_url_absolute(new_URL)) return orig_URL;
  208.  
  209.         IF (!strncmp(new_URL,"//", 2))
  210.         {
  211.                 strcpy(#newurl, "http:");
  212.                 strcat(#newurl, new_URL);
  213.                 strcpy(orig_URL, #newurl);
  214.                 return orig_URL;
  215.         }
  216.        
  217.         IF (!strncmp(new_URL,"./", 2)) new_URL+=2;
  218.  
  219.         if (ESBYTE[new_URL] == '/') //remove everything after site domain name
  220.         {
  221.                 i = strchr(#newurl+8, '/');
  222.                 if (i) ESBYTE[i]=0;
  223.                 new_URL+=1;
  224.         }
  225.                
  226.         _CUT_ST_LEVEL_MARK:
  227.                
  228.         if (newurl[strrchr(#newurl, '/')-2]<>'/')
  229.         {
  230.                 newurl[strrchr(#newurl, '/')] = 0x00;
  231.         }
  232.        
  233.         IF (!strncmp(new_URL,"../",3))
  234.         {
  235.                 new_URL+=3;
  236.                 newurl[strrchr(#newurl, '/')-1] = 0x00;
  237.                 goto _CUT_ST_LEVEL_MARK;
  238.         }
  239.        
  240.         if (newurl[strlen(#newurl)-1]<>'/') strcat(#newurl, "/");
  241.        
  242.         strcat(#newurl, new_URL);
  243.         strcpy(orig_URL, #newurl);
  244.         return orig_URL;
  245. }
  246.  
  247.  
  248. #endif