Fixed custom headers and Basic authorization, added support for redirection, functional file download interface can be used for SW updates and more.

Dependents:   Sample_HTTPClient Sample_HTTPClient LWM2M_NanoService_Ethernet LWM2M_NanoService_Ethernet ... more

Fork of HTTPClient by Vincent Wochnik

More recent changes - added iCal processing.

Derivative of a derivative, however this one works when it comes to supplying Basic authorization to access a protected resource. Some additional changes to the debug interface to clean it up for consistency with many other components I have.

Committer:
WiredHome
Date:
Thu Aug 06 11:11:31 2015 +0000
Revision:
35:d9e2d1c96b75
Parent:
34:3556275bebf3
Child:
46:ac12edc84261
Code changes based on running through a stricter compiler.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 23:517fec8b8b99 1 #include "HTTPFile.h"
WiredHome 27:32a53068ce03 2
WiredHome 34:3556275bebf3 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 35:d9e2d1c96b75 44 if ((!m_chunked && ((size_t)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 }