Subversion Repositories Kolibri OS

Rev

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