summaryrefslogtreecommitdiff
path: root/src/http_backend_winhttp.c
blob: 936e335a0743a51e819f1f7c2bedb2ec836c852d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifdef HTTP_BACKEND_WINHTTP

#include "http.h"

#include <windows.h>
#include <winhttp.h>

#include <assert.h>

SQLITE_EXTENSION_INIT3

static char* unicode_to_utf8(LPCWSTR zWide) {
    DWORD nSize;
    char* zUtf8;
    nSize = WideCharToMultiByte(CP_UTF8, 0, zWide, -1, NULL, 0, NULL, NULL);
    if (nSize == 0) {
        return NULL;
    }
    zUtf8 = sqlite3_malloc(nSize);
    if (!zUtf8) {
        return NULL;
    }
    nSize = WideCharToMultiByte(CP_UTF8, 0, zWide, -1, zUtf8, nSize, NULL, NULL);
    if (nSize == 0) {
        sqlite3_free(zUtf8);
        return NULL;
    }
    return zUtf8;
}

static LPWSTR utf8_to_unicode(const char* zText) {
    DWORD nSize;
    LPWSTR zWide;
    nSize = MultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
    if (nSize == 0) {
        return NULL;
    }
    zWide = sqlite3_malloc(sizeof(*zWide) * nSize);
    if (!zWide) {
        return NULL;
    }
    nSize = MultiByteToWideChar(CP_UTF8, 0, zText, -1, zWide, nSize);
    if (nSize == 0) {
        sqlite3_free(zWide);
        return NULL;
    }
    return zWide;
}

static char* win32_get_last_error(DWORD code) {
    LPWSTR zWide = NULL;
    FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
                       FORMAT_MESSAGE_IGNORE_INSERTS,
                   NULL,
                   code,
                   0,
                   (LPWSTR)&zWide,
                   0,
                   NULL);
    char* zUtf8 = unicode_to_utf8(zWide);
    LocalFree(zWide);
    return zUtf8;
}

static int query_headers(HINTERNET request, DWORD dwInfoLevel, char** ppResult) {
    DWORD szWide = 0;
    LPWSTR zWide = NULL;

    WinHttpQueryHeaders(
        request, dwInfoLevel, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &szWide, WINHTTP_NO_HEADER_INDEX);

    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
        return SQLITE_ERROR;
    }

    zWide = sqlite3_malloc(szWide);
    if (!zWide) {
        return SQLITE_NOMEM;
    }

    if (!WinHttpQueryHeaders(request,
                             dwInfoLevel,
                             WINHTTP_HEADER_NAME_BY_INDEX,
                             zWide,
                             &szWide,
                             WINHTTP_NO_HEADER_INDEX)) {
        sqlite3_free(zWide);
        return SQLITE_ERROR;
    }

    *ppResult = unicode_to_utf8(zWide);

    sqlite3_free(zWide);

    return SQLITE_OK;
}

int http_do_request(http_request* req, http_response* resp, char** ppErrMsg) {
    LPWSTR zUrlWide = NULL;
    LPWSTR zHostWide = NULL;
    LPWSTR zMethodWide = NULL;
    LPWSTR zHeadersWide = NULL;
    HINTERNET session = NULL;
    HINTERNET conn = NULL;
    HINTERNET request = NULL;
    URL_COMPONENTS components;
    DWORD lastErr = 0;
    const char* errFunc = NULL;
    char* zErrMsg = NULL;
    int rc = SQLITE_ERROR;
    LPWSTR zWideResponseHeaders = NULL;
    DWORD szWideResponseHeaders = 0;
    DWORD dwSize = 0;
    DWORD dwStatusCode;

    zUrlWide = utf8_to_unicode(req->zUrl);
    if (!zUrlWide) {
        rc = SQLITE_NOMEM;
        goto error;
    }

    memset(&components, 0, sizeof(components));
    components.dwStructSize = sizeof(components);
    components.dwHostNameLength = 1;
    components.dwUrlPathLength = 1;
    if (!WinHttpCrackUrl(zUrlWide, 0, 0, &components)) {
        lastErr = GetLastError();
        errFunc = "WinHttpCrackUrl";
        goto error;
    }

    session = WinHttpOpen(L"sqlite3-http",
                          WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY,
                          WINHTTP_NO_PROXY_NAME,
                          WINHTTP_NO_PROXY_BYPASS,
                          0);

    if (!session) {
        lastErr = GetLastError();
        errFunc = "WinHttpCrackOpen";
        goto error;
    }

    // WinHttpSetStatusCallback(session,
    //     (WINHTTP_STATUS_CALLBACK)statusCallback,
    //     WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
    //     0);

    zHostWide = sqlite3_malloc(sizeof(wchar_t) * (components.dwHostNameLength + 1));
    if (!zHostWide) {
        rc = SQLITE_NOMEM;
        goto error;
    }
    memcpy(zHostWide, components.lpszHostName, sizeof(wchar_t) * components.dwHostNameLength);
    zHostWide[components.dwHostNameLength] = L'\0';

    conn = WinHttpConnect(session, zHostWide, components.nPort, 0);

    if (!conn) {
        lastErr = GetLastError();
        errFunc = "WinHttpConnect";
        goto error;
    }

    zMethodWide = utf8_to_unicode(req->zMethod);
    if (!zMethodWide) {
        rc = SQLITE_NOMEM;
        goto error;
    }

    request = WinHttpOpenRequest(conn,
                                 zMethodWide,
                                 components.lpszUrlPath,
                                 NULL,
                                 WINHTTP_NO_REFERER,
                                 WINHTTP_DEFAULT_ACCEPT_TYPES,
                                 components.nPort == 443 ? WINHTTP_FLAG_SECURE : 0);

    if (!request) {
        lastErr = GetLastError();
        errFunc = "WinHttpOpenRequest";
        goto error;
    }

    if (req->zHeaders) {
        zHeadersWide = utf8_to_unicode(req->zHeaders);
    }

    if (!WinHttpSendRequest(request,
                            zHeadersWide,
                            zHeadersWide ? -1 : 0,
                            (void*)req->pBody,
                            req->szBody,
                            req->szBody,
                            0)) {
        lastErr = GetLastError();
        errFunc = "WinHttpSendRequest";
        goto error;
    }

    if (!WinHttpReceiveResponse(request, NULL)) {
        lastErr = GetLastError();
        errFunc = "WinHttpReceiveResponse";
        goto error;
    }

    assert(resp->pBody == NULL);
    assert(resp->szBody == 0);
    for (;;) {
        DWORD dwSize = 0;
        DWORD nRead = 0;
        char* nb;
        if (!WinHttpQueryDataAvailable(request, &dwSize)) {
            lastErr = GetLastError();
            errFunc = "WinHttpQueryDataAvailable";
            goto error;
        }
        if (dwSize == 0) {
            break;
        }
        nb = sqlite3_realloc(resp->pBody, resp->szBody + dwSize);
        if (!nb) {
            rc = SQLITE_NOMEM;
            goto error;
        }
        resp->pBody = nb;
        if (!WinHttpReadData(request, (char*)resp->pBody + resp->szBody, dwSize, &nRead)) {
            lastErr = GetLastError();
            errFunc = "WinHttpReadData";
            goto error;
        }
        resp->szBody += dwSize;
        if (nRead == 0) {
            break;
        }
    }

    rc = query_headers(request, WINHTTP_QUERY_RAW_HEADERS_CRLF, &resp->zHeaders);
    if (rc != SQLITE_OK) {
        if (rc == SQLITE_ERROR) {
            lastErr = GetLastError();
            errFunc = "WinHttpQueryHeaders(WINHTTP_QUERY_RAW_HEADERS_CRLF)";
        }
        goto error;
    }
    rc = SQLITE_ERROR; // need to reset this back. Otherwise a jump to error below
                       // could leave rc as SQLITE_OK...

    dwSize = sizeof(dwStatusCode);
    if (!WinHttpQueryHeaders(request,
                             WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
                             WINHTTP_HEADER_NAME_BY_INDEX,
                             &dwStatusCode,
                             &dwSize,
                             WINHTTP_NO_HEADER_INDEX)) {
        lastErr = GetLastError();
        errFunc = "WinHttpQueryHeaders(WINHTTP_QUERY_STATUS_CODE)";
        goto error;
    }

    resp->iStatusCode = dwStatusCode;

    remove_all_but_last_headers(resp->zHeaders);
    separate_status_and_headers(&resp->zStatus, resp->zHeaders);

    rc = SQLITE_OK;

    goto done;

error:

    if (rc == SQLITE_ERROR && errFunc) {
        *ppErrMsg = win32_get_last_error(lastErr);
    }

done:

    sqlite3_free(zUrlWide);
    sqlite3_free(zHostWide);
    sqlite3_free(zMethodWide);
    sqlite3_free(zHeadersWide);
    WinHttpCloseHandle(request);
    WinHttpCloseHandle(conn);
    WinHttpCloseHandle(session);

    return rc;
}

#endif // HTTP_BACKEND_WINHTTP