| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e79c3be commit b1dcfd4
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,8 @@ | |||
| 1 | 1 | #include "HTTPURLEncodedBodyParser.hpp" | |
| 2 | 2 | ||
| 3 | + #define CHUNKSIZE 512 | ||
| 4 | + #define MINCHUNKSIZE 64 | ||
| 5 | + | ||
| 3 | 6 | namespace httpsserver { | |
| 4 | 7 | ||
| 5 | 8 | HTTPURLEncodedBodyParser::HTTPURLEncodedBodyParser(HTTPRequest * req): | |
@@ -13,16 +16,56 @@ HTTPURLEncodedBodyParser::HTTPURLEncodedBodyParser(HTTPRequest * req): | |||
| 13 | 16 | { | |
| 14 | 17 | bodyLength = _request->getContentLength(); | |
| 15 | 18 | if (bodyLength) { | |
| 16 | - bodyBuffer = new char[bodyLength+1]; | ||
| 17 | - _request->readChars(bodyBuffer, bodyLength); | ||
| 19 | + // We know the body length. We try to read that much and give an error if it fails. | ||
| 20 | + bodyBuffer = (char *)malloc(bodyLength+1); | ||
| 21 | + if (bodyBuffer == NULL) { | ||
| 22 | + HTTPS_LOGE("HTTPURLEncodedBodyParser: out of memory"); | ||
| 23 | + return; | ||
| 24 | + } | ||
| 25 | + bodyPtr = bodyBuffer; | ||
| 26 | + size_t toRead = bodyLength; | ||
| 27 | + while(toRead > 0) { | ||
| 28 | + size_t didRead = _request->readChars(bodyPtr, toRead); | ||
| 29 | + if (didRead == 0) { | ||
| 30 | + HTTPS_LOGE("HTTPURLEncodedBodyParser: short read"); | ||
| 31 | + bodyLength = bodyPtr - bodyBuffer; | ||
| 32 | + break; | ||
| 33 | + } | ||
| 34 | + bodyPtr += didRead; | ||
| 35 | + toRead -= didRead; | ||
| 36 | + } | ||
| 37 | + } else { | ||
| 38 | + // We don't know the length. Read as much as possible. | ||
| 39 | + bodyBuffer = (char *)malloc(CHUNKSIZE+1); | ||
| 40 | + if (bodyBuffer == NULL) { | ||
| 41 | + HTTPS_LOGE("HTTPURLEncodedBodyParser: out of memory"); | ||
| 42 | + return; | ||
| 43 | + } | ||
| 18 | 44 | bodyPtr = bodyBuffer; | |
| 19 | - bodyBuffer[bodyLength] = '\0'; | ||
| 45 | + size_t bufferUsed = 0; | ||
| 46 | + size_t bufferAvailable = CHUNKSIZE; | ||
| 47 | + while(!_request->requestComplete()) { | ||
| 48 | + if (bufferAvailable < MINCHUNKSIZE) { | ||
| 49 | + bodyBuffer = (char *)realloc(bodyBuffer, bufferUsed + CHUNKSIZE+1); | ||
| 50 | + if (bodyBuffer == NULL) { | ||
| 51 | + HTTPS_LOGE("HTTPURLEncodedBodyParser: out of memory"); | ||
| 52 | + return; | ||
| 53 | + } | ||
| 54 | + bufferAvailable = CHUNKSIZE; | ||
| 55 | + } | ||
| 56 | + size_t didRead = _request->readChars(bodyBuffer+bufferUsed, bufferAvailable); | ||
| 57 | + bufferUsed += didRead; | ||
| 58 | + bufferAvailable -= didRead; | ||
| 59 | + } | ||
| 60 | + bodyLength = bufferUsed; | ||
| 20 | 61 | } | |
| 62 | + bodyPtr = bodyBuffer; | ||
| 63 | + bodyBuffer[bodyLength] = '\0'; | ||
| 21 | 64 | } | |
| 22 | 65 | ||
| 23 | 66 | HTTPURLEncodedBodyParser::~HTTPURLEncodedBodyParser() { | |
| 24 | 67 | if (bodyBuffer) { | |
| 25 | - delete[] bodyBuffer; | ||
| 68 | + free(bodyBuffer); | ||
| 26 | 69 | } | |
| 27 | 70 | bodyBuffer = NULL; | |
| 28 | 71 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments