Fork HTTPClient and Modfiy code for mbed 6.0
Dependents: mbed-demo-http-get-json
data/HTTPFile.cpp@29:9ee96efc1c20, 2014-06-14 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Jun 14 14:43:09 2014 +0000
- Revision:
- 29:9ee96efc1c20
- Parent:
- 27:32a53068ce03
- Child:
- 33:d4d1475bafc0
Fix a bug in HTTPFile write method that was corrupting the received data.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 23:517fec8b8b99 | 1 | #include "HTTPFile.h" |
WiredHome | 27:32a53068ce03 | 2 | |
WiredHome | 29:9ee96efc1c20 | 3 | //#define DEBUG "HTfi" |
WiredHome | 27:32a53068ce03 | 4 | #include <cstdio> |
WiredHome | 27:32a53068ce03 | 5 | #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) |
WiredHome | 27:32a53068ce03 | 6 | #define DBG(x, ...) std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 27:32a53068ce03 | 7 | #define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 27:32a53068ce03 | 8 | #define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 27:32a53068ce03 | 9 | #define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 27:32a53068ce03 | 10 | #else |
WiredHome | 27:32a53068ce03 | 11 | #define DBG(x, ...) |
WiredHome | 27:32a53068ce03 | 12 | #define WARN(x, ...) |
WiredHome | 27:32a53068ce03 | 13 | #define ERR(x, ...) |
WiredHome | 27:32a53068ce03 | 14 | #define INFO(x, ...) |
WiredHome | 27:32a53068ce03 | 15 | #endif |
WiredHome | 27:32a53068ce03 | 16 | |
WiredHome | 23:517fec8b8b99 | 17 | HTTPFile::HTTPFile(char* filename) { |
WiredHome | 27:32a53068ce03 | 18 | INFO("HTTPFile %s", filename); |
WiredHome | 27:32a53068ce03 | 19 | file = fopen(filename, "w"); |
WiredHome | 27:32a53068ce03 | 20 | m_chunked = false; |
WiredHome | 23:517fec8b8b99 | 21 | } |
WiredHome | 23:517fec8b8b99 | 22 | |
WiredHome | 23:517fec8b8b99 | 23 | void HTTPFile::close() { |
WiredHome | 27:32a53068ce03 | 24 | INFO("close()"); |
WiredHome | 23:517fec8b8b99 | 25 | if (file) { |
WiredHome | 27:32a53068ce03 | 26 | fclose(file); |
WiredHome | 27:32a53068ce03 | 27 | file = NULL; |
WiredHome | 23:517fec8b8b99 | 28 | } |
WiredHome | 23:517fec8b8b99 | 29 | } |
WiredHome | 23:517fec8b8b99 | 30 | |
WiredHome | 23:517fec8b8b99 | 31 | void HTTPFile::writeReset() { |
WiredHome | 27:32a53068ce03 | 32 | INFO("writeReset()"); |
WiredHome | 23:517fec8b8b99 | 33 | if (file) { |
WiredHome | 23:517fec8b8b99 | 34 | rewind(file); |
WiredHome | 23:517fec8b8b99 | 35 | } |
WiredHome | 23:517fec8b8b99 | 36 | } |
WiredHome | 23:517fec8b8b99 | 37 | |
WiredHome | 23:517fec8b8b99 | 38 | int HTTPFile::write(const char* buf, size_t len) { |
WiredHome | 27:32a53068ce03 | 39 | size_t written; |
WiredHome | 27:32a53068ce03 | 40 | INFO("write(%d,%s) m_len(%d), chunk %d", len, buf, m_len, m_chunked); |
WiredHome | 23:517fec8b8b99 | 41 | if (file) { |
WiredHome | 29:9ee96efc1c20 | 42 | written = fwrite(buf, 1, len, file); |
WiredHome | 27:32a53068ce03 | 43 | INFO(" writ:%d, ftell: %d", written, ftell(file)); |
WiredHome | 27:32a53068ce03 | 44 | if ((!m_chunked && (ftell(file) >= m_len)) || (m_chunked && !written)) { |
WiredHome | 27:32a53068ce03 | 45 | INFO("closing"); |
WiredHome | 23:517fec8b8b99 | 46 | close(); |
WiredHome | 23:517fec8b8b99 | 47 | } |
WiredHome | 23:517fec8b8b99 | 48 | } |
WiredHome | 23:517fec8b8b99 | 49 | return len; |
WiredHome | 23:517fec8b8b99 | 50 | } |
WiredHome | 23:517fec8b8b99 | 51 | |
WiredHome | 23:517fec8b8b99 | 52 | void HTTPFile::setDataType(const char* type) { |
WiredHome | 27:32a53068ce03 | 53 | INFO("setDataType(%s)", type); |
WiredHome | 23:517fec8b8b99 | 54 | } |
WiredHome | 23:517fec8b8b99 | 55 | |
WiredHome | 25:76084defa790 | 56 | //void HTTPFile::setLocation(const char * location) { |
WiredHome | 25:76084defa790 | 57 | // |
WiredHome | 25:76084defa790 | 58 | //} |
WiredHome | 24:eee214e3e806 | 59 | |
WiredHome | 23:517fec8b8b99 | 60 | void HTTPFile::setIsChunked(bool chunked) { |
WiredHome | 27:32a53068ce03 | 61 | INFO("setIsChunked(%d)", chunked); |
WiredHome | 23:517fec8b8b99 | 62 | m_chunked = chunked; |
WiredHome | 23:517fec8b8b99 | 63 | } |
WiredHome | 23:517fec8b8b99 | 64 | |
WiredHome | 23:517fec8b8b99 | 65 | void HTTPFile::setDataLen(size_t len) { |
WiredHome | 27:32a53068ce03 | 66 | INFO("setDataLen(%d)", len); |
WiredHome | 23:517fec8b8b99 | 67 | m_len = len; |
WiredHome | 23:517fec8b8b99 | 68 | } |