From 1e1519b34de372e4ca537cb5fa78df08401ee6b4 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 1 Aug 2020 14:15:09 +0300 Subject: GmRequest: Handle "data:" protocol with embedded content Just on a lark. --- src/gmrequest.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/gmrequest.c b/src/gmrequest.c index 5ee3382c..fa7a39c9 100644 --- a/src/gmrequest.c +++ b/src/gmrequest.c @@ -7,7 +7,7 @@ #include -static const int BODY_TIMEOUT = 1500; /* ms */ +static const int BODY_TIMEOUT = 3000; /* ms */ enum iGmRequestState { initialized_GmRequestState, @@ -186,7 +186,7 @@ void submit_GmRequest(iGmRequest *d) { clear_Block(&d->body); iUrl url; init_Url(&url, &d->url); - if (!cmpCStrSc_Rangecc(&url.protocol, "file", &iCaseInsensitive)) { + if (equalCase_Rangecc(&url.protocol, "file")) { iString *path = collect_String(urlDecode_String(collect_String(newRange_String(url.path)))); iFile * f = new_File(path); if (open_File(f, readOnly_FileMode)) { @@ -223,6 +223,35 @@ void submit_GmRequest(iGmRequest *d) { iNotifyAudience(d, finished, GmRequestFinished); return; } + else if (equalCase_Rangecc(&url.protocol, "data")) { + d->code = success_GmStatusCode; + iString *src = collectNewCStr_String(url.protocol.start + 5); + iRangecc header = { constBegin_String(src), constBegin_String(src) }; + while (header.end < constEnd_String(src) && *header.end != ',') { + header.end++; + } + iBool isBase64 = iFalse; + setRange_String(&d->header, header); + /* Check what's in the header. */ { + iRangecc entry = iNullRange; + while (nextSplit_Rangecc(&header, ";", &entry)) { + if (equal_Rangecc(&entry, "base64")) { + isBase64 = iTrue; + } + } + } + remove_Block(&src->chars, 0, size_Range(&header) + 1); + if (isBase64) { + set_Block(&src->chars, collect_Block(base64Decode_Block(&src->chars))); + } + else { + set_String(src, collect_String(urlDecode_String(src))); + } + set_Block(&d->body, &src->chars); + d->state = finished_GmRequestState; + iNotifyAudience(d, finished, GmRequestFinished); + return; + } d->state = receivingHeader_GmRequestState; d->req = new_TlsRequest(); iConnect(TlsRequest, d->req, readyRead, d, readIncoming_GmRequest_); -- cgit v1.2.3