summaryrefslogtreecommitdiff
path: root/http.c
blob: f337e32aad0b4adb3009b8a6bc7a2ff3e2223cb4 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874

/********** src/http.h **********/

/**
 * HTTP client extension for SQLite.
 *
 * https://github.com/oskaritimperi/sqlite-http-c
 *
 * MIT License
 *
 * Copyright (c) 2022 Oskari Timperi
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef HTTP_H
#define HTTP_H

#include "sqlite3ext.h"

typedef struct http_request http_request;
struct http_request {
    char* zMethod;
    char* zUrl;
    const void* pBody;
    sqlite3_int64 szBody;
    const char* zHeaders;
};

typedef struct http_response http_response;
struct http_response {
    void* pBody;
    sqlite3_int64 szBody;
    char* zHeaders;
    int szHeaders;
    int iStatusCode;
    char* zStatus;
};

int http_do_request(http_request* req, http_response* resp, char** ppErrMsg);

int http_next_header(const char* headers,
                     int size,
                     int* pParsed,
                     const char** ppName,
                     int* pNameSize,
                     const char** ppValue,
                     int* pValueSize);

void remove_all_but_last_headers(char* zHeaders);
void separate_status_and_headers(char** ppStatus, char* zHeaders);

#endif

/********** src/http.c **********/


SQLITE_EXTENSION_INIT1

#include <assert.h>
#include <ctype.h>
#include <string.h>

typedef struct http_vtab http_vtab;
struct http_vtab {
    sqlite3_vtab base;
    char* zMethod;
};

typedef struct http_cursor http_cursor;
struct http_cursor {
    sqlite3_vtab_cursor base;
    sqlite3_int64 iRowid;
    http_request req;
    http_response resp;
};

// If zHeaders contains headers for multiple responses, then this will strip
// headers from all but the last response.
void remove_all_but_last_headers(char* zHeaders) {
    int szHeaders = strlen(zHeaders);
    char* pLastTerminator = NULL;
    char* pSearch = zHeaders;

    for (;;) {
        char* p = strstr(pSearch, "\r\n\r\n");
        // If there isn't more terminators, we are done here
        if (!p) {
            break;
        }
        // If it's at the end of zHeaders, we are done here
        if (szHeaders - (p - zHeaders) == 4) {
            break;
        }
        pSearch = p + 4;
        pLastTerminator = p;
    }

    if (pLastTerminator) {
        pLastTerminator += 4;
        memmove(zHeaders, pLastTerminator, strlen(pLastTerminator) + 1);
    }
}

// Separate the status and header lines
void separate_status_and_headers(char** ppStatus, char* zHeaders) {
    char* cr = strchr(zHeaders, '\r');
    *ppStatus = sqlite3_mprintf("%.*s", cr - zHeaders, zHeaders);
    memmove(zHeaders, cr + 2, strlen(cr + 2) + 1);
}

static int httpConnect(sqlite3* db,
                       void* pAux,
                       int argc,
                       const char* const* argv,
                       sqlite3_vtab** ppVtab,
                       char** pzErr) {
    http_vtab* pNew;
    int rc;

    rc = sqlite3_declare_vtab(db,
                              "CREATE TABLE x(response_status TEXT, "
                              "response_status_code INT, response_headers TEXT, "
                              "response_body BLOB, request_method TEXT HIDDEN, "
                              "request_url TEXT HIDDEN, "
                              "request_headers TEXT HIDDEN, "
                              "request_body BLOB HIDDEN)");

#define HTTP_COL_RESPONSE_STATUS 0
#define HTTP_COL_RESPONSE_STATUS_CODE 1
#define HTTP_COL_RESPONSE_HEADERS 2
#define HTTP_COL_RESPONSE_BODY 3
#define HTTP_COL_REQUEST_METHOD 4
#define HTTP_COL_REQUEST_URL 5
#define HTTP_COL_REQUEST_HEADERS 6
#define HTTP_COL_REQUEST_BODY 7

    if (rc == SQLITE_OK) {
        pNew = sqlite3_malloc(sizeof(*pNew));
        *ppVtab = (sqlite3_vtab*)pNew;
        if (pNew == 0)
            return SQLITE_NOMEM;
        memset(pNew, 0, sizeof(*pNew));
        if (sqlite3_stricmp(argv[0], "http_get") == 0) {
            pNew->zMethod = sqlite3_mprintf("GET");
        } else if (sqlite3_stricmp(argv[0], "http_post") == 0) {
            pNew->zMethod = sqlite3_mprintf("POST");
        }
    }
    return rc;
}

static int httpDisconnect(sqlite3_vtab* pVtab) {
    http_vtab* p = (http_vtab*)pVtab;
    sqlite3_free(p->zMethod);
    sqlite3_free(p);
    return SQLITE_OK;
}

static int httpOpen(sqlite3_vtab* p, sqlite3_vtab_cursor** ppCursor) {
    http_cursor* pCur;
    http_vtab* pVtab = (http_vtab*)p;
    pCur = sqlite3_malloc(sizeof(*pCur));
    if (pCur == 0)
        return SQLITE_NOMEM;
    memset(pCur, 0, sizeof(*pCur));
    if (pVtab->zMethod) {
        pCur->req.zMethod = sqlite3_mprintf("%s", pVtab->zMethod);
    }
    *ppCursor = &pCur->base;
    return SQLITE_OK;
}

static int httpClose(sqlite3_vtab_cursor* cur) {
    http_cursor* pCur = (http_cursor*)cur;
    sqlite3_free(pCur->resp.pBody);
    sqlite3_free(pCur->req.zUrl);
    sqlite3_free(pCur->req.zMethod);
    sqlite3_free(pCur);
    return SQLITE_OK;
}

static int httpNext(sqlite3_vtab_cursor* cur) {
    http_cursor* pCur = (http_cursor*)cur;
    pCur->iRowid++;
    return SQLITE_OK;
}

static int httpColumn(sqlite3_vtab_cursor* cur, sqlite3_context* ctx, int i) {
    http_cursor* pCur = (http_cursor*)cur;

    switch (i) {
    case HTTP_COL_RESPONSE_STATUS:
        sqlite3_result_text(ctx, pCur->resp.zStatus, -1, SQLITE_TRANSIENT);
        break;

    case HTTP_COL_RESPONSE_STATUS_CODE:
        sqlite3_result_int(ctx, pCur->resp.iStatusCode);
        break;

    case HTTP_COL_RESPONSE_HEADERS:
        sqlite3_result_text(ctx, pCur->resp.zHeaders, -1, SQLITE_TRANSIENT);
        break;

    case HTTP_COL_RESPONSE_BODY:
        sqlite3_result_blob(ctx, pCur->resp.pBody, pCur->resp.szBody, SQLITE_TRANSIENT);
        break;

    case HTTP_COL_REQUEST_METHOD:
        sqlite3_result_text(ctx, pCur->req.zMethod, -1, SQLITE_TRANSIENT);
        break;

    case HTTP_COL_REQUEST_URL:
        sqlite3_result_text(ctx, pCur->req.zUrl, -1, SQLITE_TRANSIENT);
        break;

    case HTTP_COL_REQUEST_HEADERS:
        if (pCur->req.zHeaders) {
            sqlite3_result_text(ctx, pCur->req.zHeaders, -1, SQLITE_TRANSIENT);
        } else {
            sqlite3_result_null(ctx);
        }
        break;

    case HTTP_COL_REQUEST_BODY:
        if (pCur->req.pBody) {
            sqlite3_result_blob(ctx, pCur->req.pBody, pCur->req.szBody, SQLITE_TRANSIENT);
        } else {
            sqlite3_result_null(ctx);
        }
        break;
    }

    return SQLITE_OK;
}

static int httpRowid(sqlite3_vtab_cursor* cur, sqlite_int64* pRowid) {
    http_cursor* pCur = (http_cursor*)cur;
    *pRowid = pCur->iRowid;
    return SQLITE_OK;
}

static int httpEof(sqlite3_vtab_cursor* cur) {
    http_cursor* pCur = (http_cursor*)cur;
    return pCur->iRowid >= 1;
}

#define HTTP_FLAG_METHOD 1
#define HTTP_FLAG_URL 2
#define HTTP_FLAG_HEADERS 4
#define HTTP_FLAG_BODY 8

static int httpFilter(sqlite3_vtab_cursor* pVtabCursor,
                      int idxNum,
                      const char* idxStr,
                      int argc,
                      sqlite3_value** argv) {
    http_cursor* pCur = (http_cursor*)pVtabCursor;
    http_vtab* pVtab = (http_vtab*)pVtabCursor->pVtab;
    int bIsDo = pVtab->zMethod == NULL;
    char* zErrMsg = NULL;
    int rc;
    if (bIsDo) {
        pCur->req.zMethod = sqlite3_mprintf("%s", sqlite3_value_text(argv[0]));
        pCur->req.zUrl = sqlite3_mprintf("%s", sqlite3_value_text(argv[1]));
        if (idxNum & HTTP_FLAG_HEADERS) {
            pCur->req.zHeaders = sqlite3_mprintf("%s", sqlite3_value_text(argv[2]));
        }
        if (idxNum & HTTP_FLAG_BODY) {
            pCur->req.szBody = sqlite3_value_bytes(argv[3]);
            pCur->req.pBody = sqlite3_value_blob(argv[3]);
        }
    } else {
        pCur->req.zUrl = sqlite3_mprintf("%s", sqlite3_value_text(argv[0]));
        if (idxNum & HTTP_FLAG_HEADERS) {
            pCur->req.zHeaders = sqlite3_mprintf("%s", sqlite3_value_text(argv[1]));
        }
        if (idxNum & HTTP_FLAG_BODY) {
            pCur->req.szBody = sqlite3_value_bytes(argv[2]);
            pCur->req.pBody = sqlite3_value_blob(argv[2]);
        }
    }
    rc = http_do_request(&pCur->req, &pCur->resp, &zErrMsg);
    if (rc != SQLITE_OK) {
        sqlite3_free(pCur->base.pVtab->zErrMsg);
        pCur->base.pVtab->zErrMsg = zErrMsg;
    }
    return rc;
}

static int httpBestIndex(sqlite3_vtab* tab, sqlite3_index_info* pIdxInfo) {
    http_vtab* pTab = (http_vtab*)tab;
    int bIsDo = pTab->zMethod == NULL;
    int i;
    int idxNum = 0;
    const struct sqlite3_index_constraint* pConstraint;

    pConstraint = pIdxInfo->aConstraint;

    for (i = 0; i < pIdxInfo->nConstraint; ++i, ++pConstraint) {
        if (!pIdxInfo->aConstraint[i].usable) {
            return SQLITE_CONSTRAINT;
        }
        if (pConstraint->op != SQLITE_INDEX_CONSTRAINT_EQ) {
            return SQLITE_CONSTRAINT;
        };
        switch (pConstraint->iColumn) {
        case HTTP_COL_REQUEST_METHOD:
            if (bIsDo) {
                idxNum |= HTTP_FLAG_METHOD;
            } else {
                idxNum |= HTTP_FLAG_URL;
            }
            pIdxInfo->aConstraintUsage[i].argvIndex = 1;
            pIdxInfo->aConstraintUsage[i].omit = 1;
            break;

        case HTTP_COL_REQUEST_URL:
            if (bIsDo) {
                idxNum |= HTTP_FLAG_URL;
            } else {
                idxNum |= HTTP_FLAG_HEADERS;
            }
            pIdxInfo->aConstraintUsage[i].argvIndex = 2;
            pIdxInfo->aConstraintUsage[i].omit = 1;
            break;

        case HTTP_COL_REQUEST_HEADERS:
            if (bIsDo) {
                idxNum |= HTTP_FLAG_HEADERS;
            } else {
                idxNum |= HTTP_FLAG_BODY;
            }
            pIdxInfo->aConstraintUsage[i].argvIndex = 3;
            pIdxInfo->aConstraintUsage[i].omit = 1;
            break;

        case HTTP_COL_REQUEST_BODY:
            if (bIsDo) {
                idxNum |= HTTP_FLAG_BODY;
            } else {
                sqlite3_free(tab->zErrMsg);
                tab->zErrMsg = sqlite3_mprintf("too many arguments");
                return SQLITE_ERROR;
            }
            pIdxInfo->aConstraintUsage[i].argvIndex = 4;
            pIdxInfo->aConstraintUsage[i].omit = 1;
            break;
        }
    }

    if (bIsDo && !(idxNum & HTTP_FLAG_METHOD)) {
        sqlite3_free(tab->zErrMsg);
        tab->zErrMsg = sqlite3_mprintf("method missing");
        return SQLITE_ERROR;
    }

    if (!(idxNum & HTTP_FLAG_URL)) {
        sqlite3_free(tab->zErrMsg);
        tab->zErrMsg = sqlite3_mprintf("url missing");
        return SQLITE_ERROR;
    }

    pIdxInfo->estimatedCost = (double)1;
    pIdxInfo->estimatedRows = 1;
    pIdxInfo->idxNum = idxNum;

    return SQLITE_OK;
}

static sqlite3_module httpModule = {
    /* iVersion    */ 0,
    /* xCreate     */ 0,
    /* xConnect    */ httpConnect,
    /* xBestIndex  */ httpBestIndex,
    /* xDisconnect */ httpDisconnect,
    /* xDestroy    */ 0,
    /* xOpen       */ httpOpen,
    /* xClose      */ httpClose,
    /* xFilter     */ httpFilter,
    /* xNext       */ httpNext,
    /* xEof        */ httpEof,
    /* xColumn     */ httpColumn,
    /* xRowid      */ httpRowid,
    /* xUpdate     */ 0,
    /* xBegin      */ 0,
    /* xSync       */ 0,
    /* xCommit     */ 0,
    /* xRollback   */ 0,
    /* xFindMethod */ 0,
    /* xRename     */ 0,
    /* xSavepoint  */ 0,
    /* xRelease    */ 0,
    /* xRollbackTo */ 0,
    /* xShadowName */ 0,
};

static void httpSimpleFunc(sqlite3_context* ctx,
                           int argc,
                           sqlite3_value** argv,
                           const char* zTable,
                           const char* zColumn,
                           int bText) {
    char* zSql;
    int rc;
    sqlite3_stmt* pStmt;

    if (argc == 0) {
        sqlite3_result_error(ctx, "not enough arguments", -1);
        return;
    }

    if (argc == 1) {
        zSql = sqlite3_mprintf(
            "select \"%w\" from \"%w\"(%Q)", zColumn, zTable, sqlite3_value_text(argv[0]));
    }

    if (argc == 2) {
        zSql = sqlite3_mprintf("select \"%w\" from \"%w\"(%Q, %Q)",
                               zColumn,
                               zTable,
                               sqlite3_value_text(argv[0]),
                               sqlite3_value_text(argv[1]));
    }

    if (argc == 3) {
        zSql = sqlite3_mprintf("select \"%w\" from \"%w\"(%Q, %Q, %Q)",
                               zColumn,
                               zTable,
                               sqlite3_value_text(argv[0]),
                               sqlite3_value_text(argv[1]),
                               sqlite3_value_text(argv[2]));
    }

    if (argc >= 4) {
        zSql = sqlite3_mprintf("select \"%w\" from \"%w\"(%Q, %Q, %Q, %Q)",
                               zColumn,
                               zTable,
                               sqlite3_value_text(argv[0]),
                               sqlite3_value_text(argv[1]),
                               sqlite3_value_text(argv[2]),
                               sqlite3_value_text(argv[2]));
    }

    if (zSql == 0) {
        sqlite3_result_error_code(ctx, SQLITE_NOMEM);
        return;
    }

    rc = sqlite3_prepare_v2(sqlite3_context_db_handle(ctx), zSql, -1, &pStmt, 0);

    sqlite3_free(zSql);

    if (rc != SQLITE_OK) {
        sqlite3_result_error_code(ctx, rc);
        return;
    }

    rc = sqlite3_step(pStmt);

    if (rc == SQLITE_ROW) {
        int size = sqlite3_column_bytes(pStmt, 0);
        if (bText) {
            sqlite3_result_text(ctx, sqlite3_column_text(pStmt, 0), size, SQLITE_TRANSIENT);
        } else {
            sqlite3_result_blob(ctx, sqlite3_column_blob(pStmt, 0), size, SQLITE_TRANSIENT);
        }
        sqlite3_finalize(pStmt);
        return;
    }

    rc = sqlite3_finalize(pStmt);
    if (rc != SQLITE_OK) {
        sqlite3_result_error_code(ctx, rc);
    }
}

static void httpGetBodyFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    httpSimpleFunc(ctx, argc, argv, "http_get", "response_body", 0);
}

static void httpPostBodyFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    httpSimpleFunc(ctx, argc, argv, "http_post", "response_body", 0);
}

static void httpDoBodyFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    httpSimpleFunc(ctx, argc, argv, "http_do", "response_body", 0);
}

static void httpGetHeadersFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    httpSimpleFunc(ctx, argc, argv, "http_get", "response_headers", 1);
}

static void httpPostHeadersFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    httpSimpleFunc(ctx, argc, argv, "http_post", "response_headers", 1);
}

static void httpDoHeadersFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    httpSimpleFunc(ctx, argc, argv, "http_do", "response_headers", 1);
}

static void httpHeadersFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    char* zHeaders = NULL;
    int i;
    int size = 0;
    int offset = 0;
    if (argc < 2) {
        sqlite3_result_error(ctx, "http_headers: expected at least 2 arguments", -1);
        return;
    }
    if (argc % 2 != 0) {
        sqlite3_result_error(ctx, "http_headers: even number of arguments expected", -1);
        return;
    }
    for (i = 0; i < argc; i += 2) {
        size += sqlite3_value_bytes(argv[i]) + 2;
        size += sqlite3_value_bytes(argv[i + 1]) + 2;
    }
    zHeaders = sqlite3_malloc(size);
    if (!zHeaders) {
        sqlite3_result_error_code(ctx, SQLITE_NOMEM);
        return;
    }
    for (i = 0; i < argc; i += 2) {
        memcpy(zHeaders + offset, sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]));
        offset += sqlite3_value_bytes(argv[i]);

        memcpy(zHeaders + offset, ": ", 2);
        offset += 2;

        memcpy(
            zHeaders + offset, sqlite3_value_text(argv[i + 1]), sqlite3_value_bytes(argv[i + 1]));
        offset += sqlite3_value_bytes(argv[i + 1]);

        memcpy(zHeaders + offset, "\r\n", 2);
        offset += 2;
    }
    assert(offset == size);
    sqlite3_result_text(ctx, zHeaders, size, sqlite3_free);
}

static void httpHeadersHasFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    int zHeadersSize = sqlite3_value_bytes(argv[0]);
    const char* zHeaders = sqlite3_value_text(argv[0]);
    int zHeaderSize = sqlite3_value_bytes(argv[1]);
    const char* zHeader = sqlite3_value_text(argv[1]);
    while (zHeadersSize > 0) {
        int iParsed = 0;
        const char* pName;
        int iNameSize;
        int rc = http_next_header(zHeaders, zHeadersSize, &iParsed, &pName, &iNameSize, NULL, NULL);
        if (rc == SQLITE_ERROR) {
            sqlite3_result_error(ctx, "http_headers_has: malformed headers", -1);
            return;
        }
        if (rc == SQLITE_DONE) {
            sqlite3_result_int(ctx, 0);
            return;
        }
        zHeaders += iParsed;
        zHeadersSize -= iParsed;
        if (iNameSize == zHeaderSize && sqlite3_strnicmp(zHeader, pName, zHeaderSize) == 0) {
            sqlite3_result_int(ctx, 1);
            return;
        }
    }
    sqlite3_result_int(ctx, 0);
}

static void httpHeadersGetFunc(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    int zHeadersSize = sqlite3_value_bytes(argv[0]);
    const char* zHeaders = sqlite3_value_text(argv[0]);
    int zHeaderSize = sqlite3_value_bytes(argv[1]);
    const char* zHeader = sqlite3_value_text(argv[1]);
    while (zHeadersSize > 0) {
        int iParsed = 0;
        const char* pName;
        int iNameSize;
        const char* pValue;
        int iValueSize;
        int rc = http_next_header(
            zHeaders, zHeadersSize, &iParsed, &pName, &iNameSize, &pValue, &iValueSize);
        if (rc == SQLITE_ERROR) {
            sqlite3_result_error(ctx, "http_headers_get: malformed headers", -1);
            return;
        }
        if (rc == SQLITE_DONE) {
            sqlite3_result_int(ctx, 0);
            return;
        }
        zHeaders += iParsed;
        zHeadersSize -= iParsed;
        if (iNameSize == zHeaderSize && sqlite3_strnicmp(zHeader, pName, zHeaderSize) == 0) {
            sqlite3_result_text(ctx, pValue, iValueSize, SQLITE_TRANSIENT);
            return;
        }
    }
}

#define HTTP_HEADERS_EACH_COL_NAME 0
#define HTTP_HEADERS_EACH_COL_VALUE 1
#define HTTP_HEADERS_EACH_COL_HEADERS 2

typedef struct http_headers_each_vtab http_headers_each_vtab;
struct http_headers_each_vtab {
    sqlite3_vtab base;
};

typedef struct http_headers_each_cursor http_headers_each_cursor;
struct http_headers_each_cursor {
    sqlite3_vtab_cursor base;
    sqlite3_int64 iRowid;
    const char* zHeaders;
    int zHeadersSize;
    const char* p;
    const char* name;
    int nameSize;
    const char* value;
    int valueSize;
    int available;
    int done;
};

static int httpHeadersEachConnect(sqlite3* db,
                                  void* pAux,
                                  int argc,
                                  const char* const* argv,
                                  sqlite3_vtab** ppVtab,
                                  char** pzErr) {
    http_headers_each_vtab* pNew;
    int rc;
    rc = sqlite3_declare_vtab(db, "CREATE TABLE x(name TEXT, value TEXT, headers TEXT HIDDEN)");
    if (rc == SQLITE_OK) {
        pNew = sqlite3_malloc(sizeof(*pNew));
        *ppVtab = (sqlite3_vtab*)pNew;
        if (pNew == 0)
            return SQLITE_NOMEM;
        memset(pNew, 0, sizeof(*pNew));
    }
    return rc;
}

static int httpHeadersEachDisconnect(sqlite3_vtab* pVtab) {
    http_headers_each_vtab* p = (http_headers_each_vtab*)pVtab;
    sqlite3_free(p);
    return SQLITE_OK;
}

static int httpHeadersEachOpen(sqlite3_vtab* p, sqlite3_vtab_cursor** ppCursor) {
    http_headers_each_cursor* pCur;
    pCur = sqlite3_malloc(sizeof(*pCur));
    if (pCur == 0)
        return SQLITE_NOMEM;
    memset(pCur, 0, sizeof(*pCur));
    *ppCursor = &pCur->base;
    return SQLITE_OK;
}

static int httpHeadersEachClose(sqlite3_vtab_cursor* cur) {
    http_headers_each_cursor* pCur = (http_headers_each_cursor*)cur;
    sqlite3_free(pCur);
    return SQLITE_OK;
}

static int httpHeadersEachNext(sqlite3_vtab_cursor* cur) {
    http_headers_each_cursor* pCur = (http_headers_each_cursor*)cur;
    pCur->iRowid++;
    int nparsed = 0;
    int rc = http_next_header(pCur->p,
                              pCur->available,
                              &nparsed,
                              &pCur->name,
                              &pCur->nameSize,
                              &pCur->value,
                              &pCur->valueSize);
    pCur->p += nparsed;
    pCur->available -= nparsed;
    if (rc == SQLITE_ERROR) {
        sqlite3_free(cur->pVtab->zErrMsg);
        cur->pVtab->zErrMsg = sqlite3_mprintf("http_headers_each: malformed headers");
        return SQLITE_ERROR;
    }
    pCur->done = rc == SQLITE_DONE;
    return SQLITE_OK;
}

static int httpHeadersEachColumn(sqlite3_vtab_cursor* cur, sqlite3_context* ctx, int i) {
    http_headers_each_cursor* pCur = (http_headers_each_cursor*)cur;
    switch (i) {
    case HTTP_HEADERS_EACH_COL_NAME:
        sqlite3_result_text(ctx, pCur->name, pCur->nameSize, SQLITE_TRANSIENT);
        break;

    case HTTP_HEADERS_EACH_COL_VALUE:
        sqlite3_result_text(ctx, pCur->value, pCur->valueSize, SQLITE_TRANSIENT);
        break;

    case HTTP_HEADERS_EACH_COL_HEADERS:
        sqlite3_result_text(ctx, pCur->zHeaders, pCur->zHeadersSize, SQLITE_TRANSIENT);
        break;

    default:
        sqlite3_result_error(ctx, "unknown column", -1);
        break;
    }
    return SQLITE_OK;
}

static int httpHeadersEachRowid(sqlite3_vtab_cursor* cur, sqlite_int64* pRowid) {
    http_headers_each_cursor* pCur = (http_headers_each_cursor*)cur;
    *pRowid = pCur->iRowid;
    return SQLITE_OK;
}

static int httpHeadersEachEof(sqlite3_vtab_cursor* cur) {
    http_headers_each_cursor* pCur = (http_headers_each_cursor*)cur;
    return pCur->done;
}

static int httpHeadersEachFilter(sqlite3_vtab_cursor* pVtabCursor,
                                 int idxNum,
                                 const char* idxStr,
                                 int argc,
                                 sqlite3_value** argv) {
    http_headers_each_cursor* pCur = (http_headers_each_cursor*)pVtabCursor;
    pCur->zHeadersSize = sqlite3_value_bytes(argv[0]);
    pCur->available = pCur->zHeadersSize;
    pCur->zHeaders = sqlite3_value_text(argv[0]);
    pCur->p = pCur->zHeaders;
    int nparsed = 0;
    int rc = http_next_header(pCur->p,
                              pCur->available,
                              &nparsed,
                              &pCur->name,
                              &pCur->nameSize,
                              &pCur->value,
                              &pCur->valueSize);
    pCur->p += nparsed;
    pCur->available -= nparsed;
    pCur->iRowid = 1;
    if (rc == SQLITE_ERROR) {
        sqlite3_free(pVtabCursor->pVtab->zErrMsg);
        pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf("http_headers_each: malformed headers");
        return SQLITE_ERROR;
    }
    pCur->done = rc == SQLITE_DONE;
    return SQLITE_OK;
}

static int httpHeadersEachBestIndex(sqlite3_vtab* tab, sqlite3_index_info* pIdxInfo) {
    http_vtab* pTab = (http_vtab*)tab;
    int bHeadersSeen = 0;

    for (int i = 0; i < pIdxInfo->nConstraint; ++i) {
        if (pIdxInfo->aConstraint[i].iColumn == HTTP_HEADERS_EACH_COL_HEADERS) {
            if (!pIdxInfo->aConstraint[i].usable) {
                return SQLITE_CONSTRAINT;
            }
            pIdxInfo->aConstraintUsage[i].argvIndex = 1;
            pIdxInfo->aConstraintUsage[i].omit = 1;
            bHeadersSeen = 1;
        }
    }

    if (!bHeadersSeen) {
        sqlite3_free(tab->zErrMsg);
        tab->zErrMsg = sqlite3_mprintf("headers missing");
        return SQLITE_ERROR;
    }

    pIdxInfo->estimatedCost = (double)1;
    // pIdxInfo->estimatedRows = 1;

    return SQLITE_OK;
}

static sqlite3_module httpHeadersEachModule = {
    /* iVersion    */ 0,
    /* xCreate     */ 0,
    /* xConnect    */ httpHeadersEachConnect,
    /* xBestIndex  */ httpHeadersEachBestIndex,
    /* xDisconnect */ httpHeadersEachDisconnect,
    /* xDestroy    */ 0,
    /* xOpen       */ httpHeadersEachOpen,
    /* xClose      */ httpHeadersEachClose,
    /* xFilter     */ httpHeadersEachFilter,
    /* xNext       */ httpHeadersEachNext,
    /* xEof        */ httpHeadersEachEof,
    /* xColumn     */ httpHeadersEachColumn,
    /* xRowid      */ httpHeadersEachRowid,
    /* xUpdate     */ 0,
    /* xBegin      */ 0,
    /* xSync       */ 0,
    /* xCommit     */ 0,
    /* xRollback   */ 0,
    /* xFindMethod */ 0,
    /* xRename     */ 0,
    /* xSavepoint  */ 0,
    /* xRelease    */ 0,
    /* xRollbackTo */ 0,
    /* xShadowName */ 0,
};

static const struct Func {
    const char* name;
    void (*xFunc)(sqlite3_context*, int, sqlite3_value**);
} funcs[] = {
    {"http_get_body", httpGetBodyFunc},
    {"http_post_body", httpPostBodyFunc},
    {"http_do_body", httpDoBodyFunc},
    {"http_get_headers", httpGetHeadersFunc},
    {"http_post_headers", httpPostHeadersFunc},
    {"http_do_headers", httpDoHeadersFunc},
    {"http_headers", httpHeadersFunc},
    {"http_headers_has", httpHeadersHasFunc},
    {"http_headers_get", httpHeadersGetFunc},
    {NULL, NULL},
};

static const struct Module {
    const char* name;
    const sqlite3_module* module;
} modules[] = {
    {"http_get", &httpModule},
    {"http_post", &httpModule},
    {"http_do", &httpModule},
    {"http_headers_each", &httpHeadersEachModule},
    {NULL, NULL},
};

#ifdef _WIN32
__declspec(dllexport)
#endif
    int sqlite3_http_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi) {
    int rc = SQLITE_OK;
    SQLITE_EXTENSION_INIT2(pApi);
    int i;
    for (i = 0; funcs[i].name && rc == SQLITE_OK; ++i) {
        rc = sqlite3_create_function(
            db, funcs[i].name, -1, SQLITE_UTF8, NULL, funcs[i].xFunc, NULL, NULL);
    }
    for (i = 0; modules[i].name && rc == SQLITE_OK; ++i) {
        rc = sqlite3_create_module(db, modules[i].name, modules[i].module, 0);
    }
    return rc;
}

/********** src/http_backend_curl.c **********/

#ifdef HTTP_BACKEND_CURL


#include <assert.h>
#include <string.h>

SQLITE_EXTENSION_INIT3

#ifdef _WIN32
#include <windows.h>

void* http_dlopen(const char* zName) {
    return LoadLibraryA(zName);
}

void http_dlclose(void* library) {
    FreeLibrary(library);
}

void* http_dlsym(void* library, const char* zName) {
    return GetProcAddress(library, zName);
}
#else
#include <dlfcn.h>

void* http_dlopen(const char* zName) {
    return dlopen(zName, RTLD_NOW);
}

void http_dlclose(void* library) {
    dlclose(zName);
}

void* http_dlsym(void* library, const char* zName) {
    return dlsym(library, zName);
}
#endif

#ifndef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#endif

typedef struct CURL CURL;
typedef int CURLcode;
typedef int CURLoption;
typedef int CURLINFO;
typedef int CURLversion;

#define CURL_ERROR_SIZE 256

#define CURLE_OK 0

#define CURLOPT_ERRORBUFFER (10000 + 10)
#define CURLOPT_URL (10000 + 2)
#define CURLOPT_FOLLOWLOCATION (52)
#define CURLOPT_HTTPGET (80)
#define CURLOPT_POST (47)
#define CURLOPT_SSL_OPTIONS (216)
#define CURLOPT_WRITEFUNCTION (20000 + 11)
#define CURLOPT_WRITEDATA (10000 + 1)
#define CURLOPT_READFUNCTION (20000 + 12)
#define CURLOPT_READDATA (10000 + 9)
#define CURLOPT_HEADERFUNCTION (20000 + 79)
#define CURLOPT_HEADERDATA (10000 + 29)
#define CURLOPT_POSTFIELDS (10000 + 15)
#define CURLOPT_POSTFIELDSIZE_LARGE (30000 + 120)
#define CURLOPT_HTTPHEADER (10000 + 23)
#define CURLOPT_INFILESIZE_LARGE (30000 + 115)
#define CURLOPT_NOBODY (44)
#define CURLOPT_CUSTOMREQUEST (10000 + 36)
#define CURLOPT_PUT (54)

#define CURLSSLOPT_NATIVE_CA (1 << 4)

#define CURLVERSION_NOW 9

#define CURLINFO_RESPONSE_CODE (0x200000 + 2)

// The struct is bigger but I don't need more information for now...
struct curl_version_info_data {
    CURLversion age;
    const char* version;
    unsigned int version_num;
};
typedef struct curl_version_info_data curl_version_info_data;

struct curl_slist;

typedef CURL* (*curl_easy_init_t)();
typedef void (*curl_easy_cleanup_t)(CURL*);
typedef CURLcode (*curl_easy_setopt_t)(CURL*, CURLoption, ...);
typedef CURLcode (*curl_easy_perform_t)(CURL*);
typedef CURLcode (*curl_easy_getinfo_t)(CURL*, CURLINFO, ...);
typedef char* (*curl_version_t)();
typedef curl_version_info_data* (*curl_version_info_t)(CURLversion);
typedef struct curl_slist* (*curl_slist_append_t)(struct curl_slist*, const char*);
typedef void (*curl_slist_free_all_t)(struct curl_slist*);
typedef const char* (*curl_easy_strerror_t)(CURLcode);

struct curl_api_routines {
    void* pLibrary;
    curl_easy_init_t easy_init;
    curl_easy_cleanup_t easy_cleanup;
    curl_easy_setopt_t easy_setopt;
    curl_easy_perform_t easy_perform;
    curl_easy_getinfo_t easy_getinfo;
    curl_version_t version;
    curl_version_info_t version_info;
    curl_slist_append_t slist_append;
    curl_slist_free_all_t slist_free_all;
    curl_easy_strerror_t easy_strerror;
};

static struct curl_api_routines curl_api;

#define curl_easy_init curl_api.easy_init
#define curl_easy_cleanup curl_api.easy_cleanup
#define curl_easy_setopt curl_api.easy_setopt
#define curl_easy_perform curl_api.easy_perform
#define curl_easy_getinfo curl_api.easy_getinfo
#define curl_version curl_api.version
#define curl_version_info curl_api.version_info
#define curl_slist_append curl_api.slist_append
#define curl_slist_free_all curl_api.slist_free_all
#define curl_easy_strerror curl_api.easy_strerror

static const char* aCurlLibNames[] = {
#ifdef _WIN32
    "libcurl-x64.dll",
    "libcurl.dll",
#else
    "libcurl.so",
    "libcurl.so.4",
#endif
};

static const int szCurlLibNames = sizeof(aCurlLibNames) / sizeof(aCurlLibNames[0]);

int http_backend_curl_load(char** zErrMsg) {
    assert(zErrMsg != NULL);

    *zErrMsg = NULL;

    if (!curl_api.pLibrary) {
        for (int i = 0; i < szCurlLibNames && !curl_api.pLibrary; ++i) {
            curl_api.pLibrary = http_dlopen(aCurlLibNames[i]);
        }
    }

    if (!curl_api.pLibrary) {
        *zErrMsg = sqlite3_mprintf("failed to load curl");
        goto error;
    }

    curl_easy_init = (curl_easy_init_t)http_dlsym(curl_api.pLibrary, "curl_easy_init");
    if (!curl_easy_init) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_easy_init");
        goto error;
    }
    curl_easy_cleanup = (curl_easy_cleanup_t)http_dlsym(curl_api.pLibrary, "curl_easy_cleanup");
    if (!curl_easy_cleanup) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_easy_cleanup");
        goto error;
    }
    curl_easy_setopt = (curl_easy_setopt_t)http_dlsym(curl_api.pLibrary, "curl_easy_setopt");
    if (!curl_easy_setopt) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_easy_setopt");
        goto error;
    }
    curl_easy_perform = (curl_easy_perform_t)http_dlsym(curl_api.pLibrary, "curl_easy_perform");
    if (!curl_easy_perform) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_easy_perform");
        goto error;
    }
    curl_easy_getinfo = (curl_easy_getinfo_t)http_dlsym(curl_api.pLibrary, "curl_easy_getinfo");
    if (!curl_easy_getinfo) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_easy_getinfo");
        goto error;
    }
    curl_version = (curl_version_t)http_dlsym(curl_api.pLibrary, "curl_version");
    if (!curl_version) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_version");
        goto error;
    }
    curl_version_info = (curl_version_info_t)http_dlsym(curl_api.pLibrary, "curl_version_info");
    if (!curl_version_info) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_version_info");
        goto error;
    }
    curl_slist_append = (curl_slist_append_t)http_dlsym(curl_api.pLibrary, "curl_slist_append");
    if (!curl_slist_append) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_slist_append");
        goto error;
    }
    curl_slist_free_all =
        (curl_slist_free_all_t)http_dlsym(curl_api.pLibrary, "curl_slist_free_all");
    if (!curl_slist_free_all) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_slist_free_all");
        goto error;
    }
    curl_easy_strerror = (curl_easy_strerror_t)http_dlsym(curl_api.pLibrary, "curl_easy_strerror");
    if (!curl_easy_strerror) {
        *zErrMsg = sqlite3_mprintf("failed to load curl_easy_strerror");
        goto error;
    }

    return SQLITE_OK;

error:

    if (curl_api.pLibrary) {
        http_dlclose(curl_api.pLibrary);
        memset(&curl_api, 0, sizeof(curl_api));
    }

    return SQLITE_ERROR;
}

static size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
    http_response* pResp = (http_response*)userdata;
    char* p;
    p = sqlite3_realloc(pResp->pBody, pResp->szBody + size * nmemb);
    if (!p) {
        return 0;
    }
    pResp->pBody = p;
    memcpy((char*)pResp->pBody + pResp->szBody, ptr, size * nmemb);
    pResp->szBody += size * nmemb;
    return size * nmemb;
}

static size_t header_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
    http_response* pResp = (http_response*)userdata;
    char* p;
    p = sqlite3_realloc(pResp->zHeaders, pResp->szHeaders + size * nmemb + 1);
    if (!p) {
        return 0;
    }
    pResp->zHeaders = p;
    memcpy(pResp->zHeaders + pResp->szHeaders, ptr, size * nmemb);
    pResp->szHeaders += size * nmemb;
    pResp->zHeaders[pResp->szHeaders] = '\0';
    return size * nmemb;
}

struct readdata {
    const char* pBody;
    size_t szBody;
};

static size_t read_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
    struct readdata* pData = (struct readdata*)userdata;
    size_t szToSend = MIN(size * nmemb, pData->szBody);
    memcpy(ptr, pData->pBody, szToSend);
    pData->pBody += szToSend;
    pData->szBody -= szToSend;
    return szToSend;
}

static int
headers_to_curl_headers(struct curl_slist** pHeaders, const char* zHeaders, int szHeaders) {
    const char* name;
    int szName;
    const char* value;
    int szValue;
    while (szHeaders > 0) {
        int nParsed = 0;
        int rc = http_next_header(zHeaders, szHeaders, &nParsed, &name, &szName, &value, &szValue);
        if (rc == SQLITE_DONE) {
            break;
        } else if (rc == SQLITE_ROW) {
            char* zHeader = sqlite3_mprintf("%.*s: %.*s", szName, name, szValue, value);
            if (!zHeader) {
                return 0;
            }
            void* tmp = curl_slist_append(*pHeaders, zHeader);
            sqlite3_free(zHeader);
            if (!tmp) {
                return 0;
            }
            *pHeaders = tmp;
            szHeaders -= nParsed;
            zHeaders += nParsed;
        } else if (rc == SQLITE_ERROR) {
            return 0;
        }
    }
    return 1;
}

static int set_curl_error_message(char** ppErrMsg, CURLcode rc, const char* message) {
    *ppErrMsg = sqlite3_mprintf("%s: %s (curl error code %d)", message, curl_easy_strerror(rc), rc);
    return SQLITE_ERROR;
}

int http_do_request(http_request* req, http_response* resp, char** ppErrMsg) {
    int rc;
    CURL* curl;
    char aErrorBuf[CURL_ERROR_SIZE];
    curl_version_info_data* pCurlVersionInfo;
    long responseCode;
    struct curl_slist* headers = NULL;
    CURLcode curlrc;
    struct readdata readdata;

    rc = http_backend_curl_load(ppErrMsg);
    if (rc != SQLITE_OK) {
        return rc;
    }

    pCurlVersionInfo = curl_version_info(CURLVERSION_NOW);

    curl = curl_easy_init();
    if (!curl) {
        *ppErrMsg = sqlite3_mprintf("curl_easy_init failed");
        rc = SQLITE_ERROR;
        goto error;
    }

    memset(aErrorBuf, 0, sizeof(aErrorBuf));
    if ((curlrc = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, aErrorBuf)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    if ((curlrc = curl_easy_setopt(curl, CURLOPT_URL, req->zUrl)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    if ((curlrc = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    if (sqlite3_stricmp(req->zMethod, "GET") == 0) {
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    } else if (sqlite3_stricmp(req->zMethod, "POST") == 0) {
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_POST, 1L)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->pBody)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, req->szBody)) !=
            CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    } else if (sqlite3_stricmp(req->zMethod, "PUT") == 0) {
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_PUT, 1L)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    } else if (sqlite3_stricmp(req->zMethod, "HEAD") == 0) {
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_NOBODY, 1L)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    } else {
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }

        if (req->pBody && req->szBody) {
            if ((curlrc = curl_easy_setopt(curl, CURLOPT_PUT, 1L)) != CURLE_OK) {
                rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
                goto error;
            }
        }

        if ((curlrc = curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, req->zMethod)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    }

    if (req->pBody && req->szBody && sqlite3_stricmp(req->zMethod, "POST") != 0) {
        readdata.pBody = (const char*)req->pBody;
        readdata.szBody = (size_t)req->szBody;

        if ((curlrc = curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, req->szBody)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }

        if ((curlrc = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }

        if ((curlrc = curl_easy_setopt(curl, CURLOPT_READDATA, &readdata)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    }

    // Since 7.71.0 (Jun 24 2020). Makes life easier on Windows at least.
    if (pCurlVersionInfo->version_num >= ((7 << 16) | (71 << 8))) {
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA)) !=
            CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    }

    if ((curlrc = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    if ((curlrc = curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    if ((curlrc = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    if ((curlrc = curl_easy_setopt(curl, CURLOPT_HEADERDATA, resp)) != CURLE_OK) {
        rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
        goto error;
    }

    // Remove content-type header by default...
    headers = curl_slist_append(NULL, "content-type;");
    if (!headers) {
        *ppErrMsg = sqlite3_mprintf("curl_slist_append failed");
        rc = SQLITE_ERROR;
        goto error;
    }

    if (req->zHeaders) {
        if (!headers_to_curl_headers(&headers, req->zHeaders, strlen(req->zHeaders))) {
            *ppErrMsg = sqlite3_mprintf("failed to convert headers for curl");
            rc = SQLITE_ERROR;
            goto error;
        }
        if ((curlrc = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers)) != CURLE_OK) {
            rc = set_curl_error_message(ppErrMsg, curlrc, "curl_easy_setopt");
            goto error;
        }
    }

    if (curl_easy_perform(curl) != CURLE_OK) {
        *ppErrMsg = sqlite3_mprintf("curl_easy_perform failed: %s", aErrorBuf);
        rc = SQLITE_ERROR;
        goto error;
    }

    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
    resp->iStatusCode = responseCode;

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

    rc = SQLITE_OK;

error:

done:

    curl_easy_cleanup(curl);
    curl_slist_free_all(headers);

    return rc;
}

#endif // HTTP_BACKEND_CURL

/********** src/http_backend_dummy.c **********/

#ifdef HTTP_BACKEND_DUMMY


#include <assert.h>

SQLITE_EXTENSION_INIT3

static http_request sLastRequest;
static http_response* sResponse;
static char* sErrMsg;

void http_backend_dummy_set_response(http_response* response) {
    sResponse = response;
}

void http_backend_dummy_set_errmsg(const char* zErrMsg) {
    sErrMsg = sqlite3_mprintf("%s", zErrMsg);
}

void http_backend_dummy_reset_request() {
    sqlite3_free(sLastRequest.zMethod);
    sqlite3_free((void*)sLastRequest.pBody);
    sqlite3_free((void*)sLastRequest.zHeaders);
    memset(&sLastRequest, 0, sizeof(sLastRequest));
}

const http_request* http_backend_dummy_get_last_request() {
    return &sLastRequest;
}

int http_do_request(http_request* req, http_response* resp, char** ppErrMsg) {
    int rc = SQLITE_OK;
    if (sResponse) {
        *resp = *sResponse;
        sResponse = NULL;
    } else if (sErrMsg) {
        *ppErrMsg = sErrMsg;
        sErrMsg = NULL;
        rc = SQLITE_ERROR;
    } else {
        assert(0);
    }
    http_backend_dummy_reset_request();
    if (req->zMethod) {
        sLastRequest.zMethod = sqlite3_mprintf("%s", req->zMethod);
    }
    if (req->zUrl) {
        sLastRequest.zUrl = sqlite3_mprintf("%s", req->zUrl);
    }
    if (req->pBody) {
        sLastRequest.pBody = sqlite3_malloc(req->szBody);
        memcpy((void*)sLastRequest.pBody, req->pBody, req->szBody);
    }
    sLastRequest.szBody = req->szBody;
    if (req->zHeaders) {
        sLastRequest.zHeaders = sqlite3_mprintf("%s", req->zHeaders);
    }
    return rc;
}

#endif // HTTP_BACKEND_DUMMY

/********** src/http_backend_winhttp.c **********/

#ifdef HTTP_BACKEND_WINHTTP


#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

/********** src/http_next_header.c **********/

#include <ctype.h>
#include <sqlite3.h>

static int is_token_char(int c) {
    return isalnum(c) || (c != 0 && strchr("!#$%&'*+-.^_`|~", c));
}

static int is_header_ws(int c) {
    return c == ' ' || c == '\t';
}

static int is_vchar(int c) {
    return c >= 0x21 && c <= 0x7e;
}

// Return SQLITE_ROW if a header was extracted
// Return SQLITE_DONE if headers has been exhausted
// Return SQLITE_ERROR on malformed input
int http_next_header(const char* headers,
                     int size,
                     int* pParsed,
                     const char** ppName,
                     int* pNameSize,
                     const char** ppValue,
                     int* pValueSize) {
    const char* headersEnd = headers + size;
    const char* nameStart = NULL;
    const char* nameEnd = NULL;
    const char* valueStart = NULL;
    const char* valueEnd = NULL;
    const char* last = NULL;
    const char* p = headers;

    enum {
        StateInit,
        StateName,
        StateNameTrailingWs,
        StateValueLeadingWs,
        StateValue,
        StateCR,
        StateCheckFold,
        StateDone,
        StateError,
    };

    int state = StateInit;

    *ppName = NULL;
    *pNameSize = 0;
    *ppValue = NULL;
    *pValueSize = 0;

    if (size == 0) {
        *pParsed = 0;
        return SQLITE_DONE;
    }

    for (; p != headersEnd && state != StateDone && state != StateError; ++p) {
        switch (state) {
        case StateInit:
            // printf("StateInit %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (*p == '\r') {
                state = StateCR;
            } else if (is_header_ws(*p)) {
                // There shouldn't be whitespace here. Folding is handled in
                // StateCR/StateCheckFold.
                state = StateError;
            } else {
                nameStart = p;
                state = StateName;
            }
            break;

        case StateName:
            // printf("StateName %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (is_token_char(*p)) {
                // accept
            } else if (*p == ':') {
                nameEnd = p;
                state = StateValueLeadingWs;
            } else if (is_header_ws(*p)) {
                nameEnd = p;
                state = StateNameTrailingWs;
            } else {
                state = StateError;
            }
            break;

        case StateNameTrailingWs:
            // printf("StateNameTrailingWs %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (is_header_ws(*p)) {
                // accept and skip
            } else if (*p == ':') {
                state = StateValueLeadingWs;
            } else {
                state = StateError;
            }
            break;

        case StateValueLeadingWs:
            // printf("StateValueLeadingWs %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (is_header_ws(*p)) {
                // accept and skip
            } else if (*p == '\r') {
                // empty value
                state = StateCR;
            } else if (is_vchar(*p)) {
                valueStart = p;
                last = p;
                state = StateValue;
            } else {
                state = StateError;
            }
            break;

        case StateValue:
            // printf("StateValue %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (is_header_ws(*p)) {
                // accept but keep separate so that we can track trailing ws
            } else if (*p == '\r') {
                valueEnd = last + 1;
                state = StateCR;
            } else if (is_vchar(*p)) {
                // accept
                last = p;
            }
            break;

        case StateCR:
            // printf("StateCr %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (*p == '\n') {
                // If there's more data, check for folding.
                if (p + 1 == headers + size) {
                    state = StateDone;
                } else {
                    state = StateCheckFold;
                }
            } else {
                state = StateError;
            }
            break;

        case StateCheckFold:
            // printf("StateCheckFold %02x %c\n", *p, isalnum(*p) ? *p : ' ');
            if (is_header_ws(*p)) {
                state = StateValue;
            } else {
                state = StateDone;
                --p;
            }
            break;
        }
    }

    *pParsed = p - headers;

    if (state != StateDone || state == StateError) {
        return SQLITE_ERROR;
    }

    if (!nameStart) {
        return SQLITE_DONE;
    }

    *ppName = nameStart;
    *pNameSize = nameEnd - nameStart;

    if (valueStart) {
        *ppValue = valueStart;
        *pValueSize = valueEnd - valueStart;
    }

    return SQLITE_ROW;
}