Subversion Repositories Kolibri OS

Rev

Rev 8413 | 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.         if (streqrp(cur_url, "http://gate.aspero.pro/?site=")) cur_url += 29;
  103.         http_get stdcall (_url, 0, 0, #accept_language);
  104.         transfer = EAX;
  105.         return EAX;
  106. }
  107.  
  108. void _http::hfree()
  109. {
  110.         http_free stdcall (transfer);
  111.         transfer=0;
  112. }
  113.  
  114. bool _http::stop()
  115. {
  116.         if (transfer)
  117.         {
  118.                 /*
  119.                 EAX = transfer;
  120.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  121.                 $push   EAX                                                     // save it on the stack
  122.                 http_free stdcall (transfer);   // abort connection
  123.                 $pop    EAX                                                    
  124.                 free(EAX);                                              // free data
  125.                 transfer=0;
  126.                 */
  127.                 hfree();
  128.                 return true;    
  129.         }
  130.         return false;
  131. }
  132.  
  133. void _http::receive()
  134. {
  135.         http_receive stdcall (transfer);
  136.         receive_result = EAX;
  137.  
  138.         EDI = transfer;
  139.         if (!EAX) {
  140.                 status_code = EDI.http_msg.status;
  141.                 content_pointer = EDI.http_msg.content_ptr;
  142.         }
  143.         content_length = EDI.http_msg.content_length;
  144.         content_received = EDI.http_msg.content_received;
  145. }
  146.  
  147. :dword _http::header_field(dword _header_field, _dst, _size)
  148. {
  149.         http_find_header_field stdcall (transfer, _header_field);
  150.         if (EAX!=0) {
  151.                 ESI = EAX;
  152.                 EDI = _dst;
  153.                 EDX = _size;
  154.                 do {
  155.                         $lodsb;
  156.                         $stosb;
  157.                         $dec edx
  158.                 } while (AL != 0) && (AL != 13) && (AL != 10) && (EDX>0);
  159.                 DSBYTE[EDI-1]='\0';
  160.                 return _dst;
  161.         }
  162.         return NULL;
  163. }
  164.  
  165.  
  166. //===================================================//
  167. //                                                   //
  168. //                 CHECK PATH TYPE                   //
  169. //                                                   //
  170. //===================================================//
  171.  
  172. :int check_is_the_adress_local(dword _in)
  173. {
  174.         if (ESBYTE[_in]!='/') return false;
  175.         _in++;
  176.         if(!strncmp(_in,"rd/",3)) return true;
  177.         if(!strncmp(_in,"fd/",3)) return true;
  178.         if(!strncmp(_in,"hd",2)) return true;
  179.         if(!strncmp(_in,"bd",2)) return true;
  180.         if(!strncmp(_in,"cd",2)) return true;
  181.         if(!strncmp(_in,"sys/",4)) return true;
  182.         if(!strncmp(_in,"tmp",3)) return true;
  183.         if(!strncmp(_in,"usbhd",5)) return true;
  184.         if(!strncmp(_in,"kolibrios",9)) return true;
  185.         return false;
  186. }
  187.  
  188. :int check_is_the_url_absolute(dword _in)
  189. {
  190.         if(!strncmp(_in,"ftp:",4)) return true;
  191.         if(!strncmp(_in,"http:",5)) return true;
  192.         if(!strncmp(_in,"https:",6)) return true;
  193.         if(!strncmp(_in,"mailto:",7)) return true;
  194.         if(!strncmp(_in,"tel:",4)) return true;
  195.         if(!strncmp(_in,"#",1)) return true;
  196.         if(!strncmp(_in,"WebView:",8)) return true;
  197.         if(check_is_the_adress_local(_in)) return true;
  198.         return false;
  199. }
  200.  
  201. :dword get_absolute_url(dword new_URL, base_URL)
  202. {
  203.         int i;
  204.         dword orig_URL = new_URL;
  205.         char newurl[URL_SIZE+1];
  206.         strcpy(#newurl, base_URL);
  207.  
  208.         while (i=strstr(new_URL, "&amp;")) strcpy(i+1, i+5);
  209.  
  210.         if (check_is_the_url_absolute(new_URL)) return new_URL;
  211.  
  212.         switch(ESBYTE[new_URL]) {
  213.                 case '.':
  214.                         new_URL++;
  215.                         if (ESBYTE[new_URL] == '/') {
  216.                                 new_URL++;
  217.                         }
  218.                         else if (ESBYTE[new_URL] == '.') && (ESBYTE[new_URL+1] == '/') {
  219.                                 _GO_UP:
  220.                                 new_URL+=2;
  221.                                 newurl[strrchr(#newurl, '/')-1] = '\0';                        
  222.                         }
  223.                         goto _DEFAULT;
  224.                 case '?':
  225.                         strchr(#newurl+8, '?'); //returns EAX
  226.                         if (EAX) ESBYTE[EAX] = '\0';
  227.                         break;
  228.                 case '/':
  229.                         new_URL++;
  230.                         if (ESBYTE[new_URL] == '/') {
  231.                                 strcpy(#newurl, "http:/");
  232.                                 break;
  233.                         }
  234.                         EAX = strchr(#newurl+8, '/');
  235.                         if (EAX) ESBYTE[EAX] = '\0';
  236.                 default:
  237.                         _DEFAULT:
  238.                         EAX = strrchr(#newurl, '/');
  239.                         if (newurl[EAX-2]!='/') newurl[EAX] = '\0';
  240.                         if (!strncmp(new_URL,"../",3)) goto _GO_UP;
  241.                         if (newurl[strlen(#newurl)-1]!='/') strncat(#newurl, "/", URL_SIZE);
  242.         }
  243.         strncat(#newurl, new_URL, URL_SIZE);
  244.         strncpy(orig_URL, #newurl, URL_SIZE);
  245.         return orig_URL;
  246. }
  247.  
  248.  
  249. #endif