Subversion Repositories Kolibri OS

Rev

Rev 8319 | Rev 8410 | 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.                 return _dst;
  157.         }
  158.         return NULL;
  159. }
  160.  
  161.  
  162. //===================================================//
  163. //                                                   //
  164. //                 CHECK PATH TYPE                   //
  165. //                                                   //
  166. //===================================================//
  167.  
  168. :int check_is_the_adress_local(dword _in)
  169. {
  170.         if (ESBYTE[_in]!='/') return false;
  171.         _in++;
  172.         if(!strncmp(_in,"rd/",3)) return true;
  173.         if(!strncmp(_in,"fd/",3)) return true;
  174.         if(!strncmp(_in,"hd",2)) return true;
  175.         if(!strncmp(_in,"bd",2)) return true;
  176.         if(!strncmp(_in,"cd",2)) return true;
  177.         if(!strncmp(_in,"sys/",4)) return true;
  178.         if(!strncmp(_in,"tmp",3)) return true;
  179.         if(!strncmp(_in,"usbhd",5)) return true;
  180.         if(!strncmp(_in,"kolibrios",9)) return true;
  181.         return false;
  182. }
  183.  
  184. :int check_is_the_url_absolute(dword _in)
  185. {
  186.         if(!strncmp(_in,"ftp:",4)) return true;
  187.         if(!strncmp(_in,"http:",5)) return true;
  188.         if(!strncmp(_in,"https:",6)) return true;
  189.         if(!strncmp(_in,"mailto:",7)) return true;
  190.         if(!strncmp(_in,"tel:",4)) return true;
  191.         if(!strncmp(_in,"#",1)) return true;
  192.         if(!strncmp(_in,"WebView:",8)) return true;
  193.         if(check_is_the_adress_local(_in)) return true;
  194.         return false;
  195. }
  196.  
  197. :dword get_absolute_url(dword new_URL, base_URL)
  198. {
  199.         int i;
  200.         dword orig_URL = new_URL;
  201.         char newurl[URL_SIZE+1];
  202.         strcpy(#newurl, base_URL);
  203.  
  204.         while (i=strstr(new_URL, "&amp;")) strcpy(i+1, i+5);
  205.  
  206.         if (check_is_the_url_absolute(new_URL)) return orig_URL;
  207.  
  208.         IF (!strncmp(new_URL,"//", 2))
  209.         {
  210.                 strncpy(#newurl, "http:", URL_SIZE);
  211.                 strncat(#newurl, new_URL, URL_SIZE);
  212.                 strncpy(orig_URL, #newurl, URL_SIZE);
  213.                 return orig_URL;
  214.         }
  215.        
  216.         IF (!strncmp(new_URL,"./", 2)) new_URL+=2;
  217.  
  218.         if (ESBYTE[new_URL] == '/') //remove everything after site domain name
  219.         {
  220.                 i = strchr(#newurl+8, '/');
  221.                 if (i) ESBYTE[i]=0;
  222.                 new_URL+=1;
  223.         }
  224.                
  225.         _CUT_ST_LEVEL_MARK:
  226.                
  227.         if (newurl[strrchr(#newurl, '/')-2]<>'/')
  228.         {
  229.                 newurl[strrchr(#newurl, '/')] = 0x00;
  230.         }
  231.        
  232.         IF (!strncmp(new_URL,"../",3))
  233.         {
  234.                 new_URL+=3;
  235.                 newurl[strrchr(#newurl, '/')-1] = 0x00;
  236.                 goto _CUT_ST_LEVEL_MARK;
  237.         }
  238.        
  239.         if (newurl[strlen(#newurl)-1]<>'/') strncat(#newurl, "/", URL_SIZE);
  240.        
  241.         strncat(#newurl, new_URL, URL_SIZE);
  242.         strncpy(orig_URL, #newurl, URL_SIZE);
  243.         return orig_URL;
  244. }
  245.  
  246.  
  247. #endif