naett.c

Tiny cross-platform HTTP / HTTPS client library in C.
git clone git://git.finwo.net/lib/naett.c
Log | Files | Refs | README | LICENSE

naett_internal.h (2254B)


      1 #ifndef NAETT_INTERNAL_H
      2 #define NAETT_INTERNAL_H
      3 
      4 #ifdef _MSC_VER
      5     #define strcasecmp _stricmp
      6     #undef strdup
      7     #define strdup _strdup
      8 #endif
      9 
     10 #ifdef _WIN32
     11 #define WIN32_LEAN_AND_MEAN
     12 #define NOMINMAX
     13 #include <windows.h>
     14 #include <winhttp.h>
     15 #ifndef min
     16     #define min(x,y) ((x) < (y) ? (x) : (y))
     17 #endif
     18 #define __WINDOWS__ 1
     19 #endif
     20 
     21 #if __linux__ && !__ANDROID__
     22 #define __LINUX__ 1
     23 #include <curl/curl.h>
     24 #endif
     25 
     26 #if __ANDROID__
     27 #include <jni.h>
     28 #include <pthread.h>
     29 #endif
     30 
     31 #ifdef __APPLE__
     32 #include "TargetConditionals.h"
     33 #include <objc/objc.h>
     34 #if TARGET_OS_IPHONE
     35 #define __IOS__ 1
     36 #else
     37 #define __MACOS__ 1
     38 #endif
     39 #endif
     40 
     41 #define naettAlloc(TYPE, VAR) TYPE* VAR = (TYPE*)calloc(1, sizeof(TYPE))
     42 
     43 typedef struct KVLink {
     44     const char* key;
     45     const char* value;
     46     struct KVLink* next;
     47 } KVLink;
     48 
     49 typedef struct Buffer {
     50     void* data;
     51     int size;
     52     int capacity;
     53     int position;
     54 } Buffer;
     55 
     56 typedef struct {
     57     const char* method;
     58     const char* userAgent;
     59     int timeoutMS;
     60     naettReadFunc bodyReader;
     61     void* bodyReaderData;
     62     naettWriteFunc bodyWriter;
     63     void* bodyWriterData;
     64     KVLink* headers;
     65     Buffer body;
     66 } RequestOptions;
     67 
     68 typedef struct {
     69     RequestOptions options;
     70     const char* url;
     71 #if __APPLE__
     72     id urlRequest;
     73 #endif
     74 #if __ANDROID__
     75     jobject urlObject;
     76 #endif
     77 #if __WINDOWS__
     78     HINTERNET session;
     79     HINTERNET connection;
     80     HINTERNET request;
     81     LPWSTR host;
     82     LPWSTR resource;
     83 #endif
     84 } InternalRequest;
     85 
     86 typedef struct {
     87     InternalRequest* request;
     88     int code;
     89     int complete;
     90     KVLink* headers;
     91     Buffer body;
     92     int contentLength;  // 0 if headers not read, -1 if Content-Length missing.
     93     int totalBytesRead;
     94 #if __APPLE__
     95     id session;
     96 #endif
     97 #if __ANDROID__
     98     pthread_t workerThread;
     99     int closeRequested;
    100 #endif
    101 #if __LINUX__
    102     struct curl_slist* headerList;
    103 #endif
    104 #if __WINDOWS__
    105     char buffer[10240];
    106     size_t bytesLeft;
    107 #endif
    108 } InternalResponse;
    109 
    110 void naettPlatformInit(naettInitData initData);
    111 int naettPlatformInitRequest(InternalRequest* req);
    112 void naettPlatformMakeRequest(InternalResponse* res);
    113 void naettPlatformFreeRequest(InternalRequest* req);
    114 void naettPlatformCloseResponse(InternalResponse* res);
    115 
    116 #endif  // NAETT_INTERNAL_H