Subversion Repositories Kolibri OS

Rev

Rev 9811 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.     This is adapded thunk for http.obj sys library
  3.     .h is equal to svn:\\programs\develop\libraries\http\http_en.txt
  4.  
  5.     Adapted for TCC's dynamic API by Magomed Kostoev, 2020
  6. */
  7.  
  8. #ifndef KOLIBRI_HTTP_H
  9. #define KOLIBRI_HTTP_H
  10.  
  11. #include <stddef.h>
  12.  
  13. // Bitflags for http_msg.flags
  14. // status
  15.  
  16. #define HTTP_FLAG_HTTP11             1 << 0
  17. #define HTTP_FLAG_GOT_HEADER         1 << 1
  18. #define HTTP_FLAG_GOT_ALL_DATA       1 << 2
  19. #define HTTP_FLAG_CONTENT_LENGTH     1 << 3
  20. #define HTTP_FLAG_CHUNKED            1 << 4
  21. #define HTTP_FLAG_CONNECTED          1 << 5
  22.  
  23. // user options
  24. #define HTTP_FLAG_KEEPALIVE          1 << 8
  25. #define HTTP_FLAG_STREAM             1 << 9
  26. #define HTTP_FLAG_REUSE_BUFFER       1 << 10
  27. #define HTTP_FLAG_BLOCK              1 << 11
  28.  
  29. // error
  30. #define HTTP_FLAG_INVALID_HEADER     1 << 16
  31. #define HTTP_FLAG_NO_RAM             1 << 17
  32. #define HTTP_FLAG_SOCKET_ERROR       1 << 18
  33. #define HTTP_FLAG_TIMEOUT_ERROR      1 << 19
  34. #define HTTP_FLAG_TRANSFER_FAILED    1 << 20
  35.  
  36. /*
  37. User flags:
  38.  
  39. For the flag codes themselves, see http.inc file.
  40.  
  41.  FLAG_KEEPALIVE will keep the connection open after first GET/POST/.. so you can send a second request on the same TCP session.
  42. In this case, the session must be closed manually when done by using the exported disconnect() function.
  43.  
  44.  FLAG_STREAM will force receive() to put the received content in a series of fixed size buffers, instead of everything in one big buffer.
  45. This can be used for example to receive an internet radio stream,
  46. but also to download larger files for which it does not make sense to put them completely in RAM first.
  47.  
  48.  FLAG_REUSE_BUFFER is to be used in combination with FLAG_STREAM and will make receive() function re-use the same buffer.
  49. This, for example, can be used when downloading a file straight to disk.
  50.  
  51.  FLAG_BLOCK will make receive() function blocking. This is only to be used when receiving one file from a thread that has no other work.
  52. If however, you want to receive multiple files, or do other things in the program mainloop, you should call the receive function periodically.
  53. You may use system function 10 or 23 to wait for network event before calling one or more receive() functions.
  54. */
  55.  
  56. #pragma pack(push,1)
  57. typedef struct http_msg_s {
  58.     unsigned socket;           // socket on which the actual transfer happens
  59.     unsigned flags;            // flags, reflects status of the transfer using bitflags
  60.     unsigned write_ptr;        // internal use only (where to write new data in buffer)
  61.     unsigned buffer_length;    // internal use only (number of available bytes in buffer)
  62.     unsigned chunk_ptr;        // internal use only (where the next chunk begins)
  63.     unsigned timestamp;        // internal use only (when last data was received)
  64.     unsigned status;           // HTTP status
  65.     unsigned header_length;    // length of HTTP header
  66.     void *   content_ptr;      // ptr to content
  67.     unsigned content_length;   // total length of HTTP content
  68.     unsigned content_received; // number of currently received content bytes
  69.     char *   http_header;
  70. } http_msg;
  71. #pragma pack(pop)
  72.  
  73. /*
  74.     url = pointer to ASCIIZ URL
  75.     identifier = identifier of previously opened connection (keep-alive), or 0 to open a new one.
  76.     flags = bit flags (see end of this document).
  77.     add_header = pointer to ASCIIZ additional header parameters, or null for none.
  78.     Every additional parameter must end with CR LF bytes, including the last line.
  79.     Initiates a HTTP connection, using 'GET' method.
  80.     Returns NULL on error, identifier otherwise.
  81. */
  82. DLLAPI http_msg * __stdcall http_get(const char *url, http_msg *identifier, unsigned flags, const char *add_header);
  83.  
  84. /*
  85.     url = pointer to ASCIIZ URL
  86.     identifier = identifier of previously opened connection (keep-alive), or 0 to open a new one.
  87.     flags = bit flags (see end of this document).
  88.     add_header = pointer to ASCIIZ additional header parameters, or null for none.    
  89.     Every additional parameter must end with CR LF bytes, including the last line.    
  90.     Initiate a HTTP connection, using 'HEAD' method.
  91.     Returns NULL on error, identifier otherwise.
  92. */
  93. DLLAPI http_msg * __stdcall http_head(const char *url, http_msg *identifier, unsigned flags, const char *add_header);
  94.  
  95. /*
  96.     url = pointer to ASCIIZ URL
  97.     identifier = identifier of previously opened connection (keep-alive), or 0 to open a new one.
  98.     flags = bit flags (see end of this document).
  99.     add_header = pointer to ASCIIZ additional header parameters, or null for none.    
  100.     Every additional parameter must end with CR LF bytes, including the last line.
  101.     content-type = pointer to ASCIIZ string containing content type.
  102.     content-length = length of the content (in bytes).
  103.     Initiate a HTTP connection, using 'POST' method.
  104.     The content itself must be send to the socket (which you can find in the structure),
  105.     using system function 75, 6.
  106.     Returns 0 on error, identifier otherwise
  107. */
  108. DLLAPI http_msg * __stdcall http_post(const char *url, http_msg *identifier, unsigned flags, const char *add_header,
  109.                                   const char *content_type, unsigned content_length);
  110.  
  111. /*
  112.     identifier = identifier which one of the previous functions returned
  113.     This procedure will handle all incoming data for a connection and place it in the buffer.
  114.     As long as the procedure expects more data, -1 is returned and the procedure must be called again.
  115.     When transfer is done, the procedure will return 0.
  116.     The receive procedure is non-blocking by default, but can be made to block by setting FLAG_BLOCK.
  117.  
  118.     The HTTP header is placed together with some flags and other attributes in the http_msg structure.
  119.     This structure is defined in http.inc (and not copied here because it might still change.)
  120.     The identifier used by the functions is actually a pointer to this structure.
  121.     In the dword named .flags, the library will set various bit-flags indicating the status of the process.
  122.     (When a transfer is done, one should check these bit-flags to find out if the transfer was error-free.)
  123.     The HTTP header is placed at the end of this structure. The content is placed in another buffer.
  124.     The dword .status contains the status code received from the server (e.g. 200 for OK).
  125.     In header_length you'll find the length of the header as soon as it has been received.
  126.     In content_ptr you'll find a pointer to the actual content.
  127.     In content_length you'll find the length of the content.
  128.     In content_received, you'll find the number of content bytes already received.
  129. */
  130. DLLAPI int __stdcall http_receive(http_msg *identifier);
  131.  
  132. /*
  133.     identifier = identifier which one of the previous functions returned
  134.     dataptr = pointer to the data you want to send
  135.     datalength = length of the data to send (in bytes)
  136.     This procedure can be used to send data to the server (POST)
  137.     Returns number of bytes sent, -1 on error
  138. */
  139. DLLAPI int __stdcall http_send(http_msg *identifier, void *dataptr, unsigned datalength);
  140.  
  141. /*
  142.     Sometimes the http_receive function receives incomplete data. If you have the same problem then a macro can help you:
  143. */
  144.  
  145. DLLAPI int __stdcall http_free(http_msg *identifier);
  146. /*
  147.     Free unused data
  148. */
  149.  
  150. #define http_long_receive(x) while(http_receive(x)){};
  151.  
  152. #endif // KOLIBRI_HTTP_H
  153.