Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/
Dependencies: BlinkLed HTTPClient EthernetInterface FatFileSystemCpp MSCFileSystem mbed-rtos mbed
Download NHK English news podcast automatically. This application requires mpod mother board. See also http://mbed.org/users/geodenx/notebook/mpod/
HTTPFile.cpp@0:1855a008f28e, 2012-08-16 (annotated)
- Committer:
- togayan
- Date:
- Thu Aug 16 15:49:19 2012 +0000
- Revision:
- 0:1855a008f28e
- Child:
- 4:ab3092d15121
First revision of mpod_nhk_english.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
togayan | 0:1855a008f28e | 1 | /* HTTPFile.cpp */ |
togayan | 0:1855a008f28e | 2 | #include "HTTPFile.h" |
togayan | 0:1855a008f28e | 3 | |
togayan | 0:1855a008f28e | 4 | #include <cstring> |
togayan | 0:1855a008f28e | 5 | |
togayan | 0:1855a008f28e | 6 | #define OK 0 |
togayan | 0:1855a008f28e | 7 | #define NG -1 |
togayan | 0:1855a008f28e | 8 | |
togayan | 0:1855a008f28e | 9 | using std::memcpy; |
togayan | 0:1855a008f28e | 10 | using std::strncpy; |
togayan | 0:1855a008f28e | 11 | using std::strlen; |
togayan | 0:1855a008f28e | 12 | |
togayan | 0:1855a008f28e | 13 | HTTPFile::HTTPFile(const char* path) : |
togayan | 0:1855a008f28e | 14 | m_fp(NULL), |
togayan | 0:1855a008f28e | 15 | m_path(path), |
togayan | 0:1855a008f28e | 16 | m_len(0), |
togayan | 0:1855a008f28e | 17 | m_chunked(false) |
togayan | 0:1855a008f28e | 18 | { |
togayan | 0:1855a008f28e | 19 | } |
togayan | 0:1855a008f28e | 20 | |
togayan | 0:1855a008f28e | 21 | HTTPFile::~HTTPFile() |
togayan | 0:1855a008f28e | 22 | { |
togayan | 0:1855a008f28e | 23 | closeFile(); |
togayan | 0:1855a008f28e | 24 | } |
togayan | 0:1855a008f28e | 25 | |
togayan | 0:1855a008f28e | 26 | void HTTPFile::clear() |
togayan | 0:1855a008f28e | 27 | { |
togayan | 0:1855a008f28e | 28 | closeFile(); |
togayan | 0:1855a008f28e | 29 | //Force reopening |
togayan | 0:1855a008f28e | 30 | } |
togayan | 0:1855a008f28e | 31 | |
togayan | 0:1855a008f28e | 32 | /*virtual*/ int HTTPFile::read(char* buf, size_t len, size_t* pReadLen) |
togayan | 0:1855a008f28e | 33 | { |
togayan | 0:1855a008f28e | 34 | if(!openFile("r")) //File does not exist, or I/O error... |
togayan | 0:1855a008f28e | 35 | return NG; |
togayan | 0:1855a008f28e | 36 | |
togayan | 0:1855a008f28e | 37 | *pReadLen = fread(buf, 1, len, m_fp); |
togayan | 0:1855a008f28e | 38 | if( feof(m_fp) ) |
togayan | 0:1855a008f28e | 39 | { |
togayan | 0:1855a008f28e | 40 | //File read completely, we can close it |
togayan | 0:1855a008f28e | 41 | closeFile(); |
togayan | 0:1855a008f28e | 42 | } |
togayan | 0:1855a008f28e | 43 | return OK; |
togayan | 0:1855a008f28e | 44 | } |
togayan | 0:1855a008f28e | 45 | |
togayan | 0:1855a008f28e | 46 | /*virtual*/ int HTTPFile::write(const char* buf, size_t len) |
togayan | 0:1855a008f28e | 47 | { |
togayan | 0:1855a008f28e | 48 | if(!openFile("w")) //File does not exist, or I/O error... |
togayan | 0:1855a008f28e | 49 | return NG; |
togayan | 0:1855a008f28e | 50 | |
togayan | 0:1855a008f28e | 51 | len = fwrite(buf, 1, len, m_fp); |
togayan | 0:1855a008f28e | 52 | //DBG("Written %d bytes in %d\n", len, m_fp); |
togayan | 0:1855a008f28e | 53 | if( (!m_chunked && (ftell(m_fp) >= m_len)) || (m_chunked && !len) ) |
togayan | 0:1855a008f28e | 54 | { |
togayan | 0:1855a008f28e | 55 | //File received completely, we can close it |
togayan | 0:1855a008f28e | 56 | closeFile(); |
togayan | 0:1855a008f28e | 57 | } |
togayan | 0:1855a008f28e | 58 | return len; |
togayan | 0:1855a008f28e | 59 | } |
togayan | 0:1855a008f28e | 60 | |
togayan | 0:1855a008f28e | 61 | /*virtual*/ int HTTPFile::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header |
togayan | 0:1855a008f28e | 62 | { |
togayan | 0:1855a008f28e | 63 | strncpy(type, "text/plain", maxTypeLen-1); |
togayan | 0:1855a008f28e | 64 | type[maxTypeLen-1] = '\0'; |
togayan | 0:1855a008f28e | 65 | return OK; |
togayan | 0:1855a008f28e | 66 | } |
togayan | 0:1855a008f28e | 67 | |
togayan | 0:1855a008f28e | 68 | /*virtual*/ void HTTPFile::setDataType(const char* type) //Internet media type from Content-Type header |
togayan | 0:1855a008f28e | 69 | { |
togayan | 0:1855a008f28e | 70 | //Do not really care here |
togayan | 0:1855a008f28e | 71 | } |
togayan | 0:1855a008f28e | 72 | |
togayan | 0:1855a008f28e | 73 | /*virtual*/ bool HTTPFile::getIsChunked() //For Transfer-Encoding header |
togayan | 0:1855a008f28e | 74 | { |
togayan | 0:1855a008f28e | 75 | return false; |
togayan | 0:1855a008f28e | 76 | } |
togayan | 0:1855a008f28e | 77 | |
togayan | 0:1855a008f28e | 78 | /*virtual*/ void HTTPFile::setIsChunked(bool chunked) //For Transfer-Encoding header |
togayan | 0:1855a008f28e | 79 | { |
togayan | 0:1855a008f28e | 80 | m_chunked = chunked; |
togayan | 0:1855a008f28e | 81 | } |
togayan | 0:1855a008f28e | 82 | |
togayan | 0:1855a008f28e | 83 | /*virtual*/ size_t HTTPFile::getDataLen() //For Content-Length header |
togayan | 0:1855a008f28e | 84 | { |
togayan | 0:1855a008f28e | 85 | return m_len; |
togayan | 0:1855a008f28e | 86 | } |
togayan | 0:1855a008f28e | 87 | |
togayan | 0:1855a008f28e | 88 | /*virtual*/ void HTTPFile::setDataLen(size_t len) //For Content-Length header, or if the transfer is chunked, next chunk length |
togayan | 0:1855a008f28e | 89 | { |
togayan | 0:1855a008f28e | 90 | if(!m_chunked) |
togayan | 0:1855a008f28e | 91 | m_len = len; //Useful so that we can close file when last byte is written |
togayan | 0:1855a008f28e | 92 | } |
togayan | 0:1855a008f28e | 93 | |
togayan | 0:1855a008f28e | 94 | bool HTTPFile::openFile(const char* mode) //true on success, false otherwise |
togayan | 0:1855a008f28e | 95 | { |
togayan | 0:1855a008f28e | 96 | if(m_fp) |
togayan | 0:1855a008f28e | 97 | return true; |
togayan | 0:1855a008f28e | 98 | |
togayan | 0:1855a008f28e | 99 | m_fp = fopen(m_path.c_str(), mode); |
togayan | 0:1855a008f28e | 100 | if(m_fp && mode[0]=='r') |
togayan | 0:1855a008f28e | 101 | { |
togayan | 0:1855a008f28e | 102 | //Seek EOF to get length |
togayan | 0:1855a008f28e | 103 | fseek(m_fp, 0, SEEK_END); |
togayan | 0:1855a008f28e | 104 | m_len = ftell(m_fp); |
togayan | 0:1855a008f28e | 105 | fseek(m_fp, 0, SEEK_SET); //Goto SOF |
togayan | 0:1855a008f28e | 106 | } |
togayan | 0:1855a008f28e | 107 | |
togayan | 0:1855a008f28e | 108 | //DBG("fd = %d\n", m_fp); |
togayan | 0:1855a008f28e | 109 | |
togayan | 0:1855a008f28e | 110 | if(!m_fp) |
togayan | 0:1855a008f28e | 111 | return false; |
togayan | 0:1855a008f28e | 112 | |
togayan | 0:1855a008f28e | 113 | return true; |
togayan | 0:1855a008f28e | 114 | } |
togayan | 0:1855a008f28e | 115 | |
togayan | 0:1855a008f28e | 116 | void HTTPFile::closeFile() |
togayan | 0:1855a008f28e | 117 | { |
togayan | 0:1855a008f28e | 118 | if(m_fp) |
togayan | 0:1855a008f28e | 119 | fclose(m_fp); |
togayan | 0:1855a008f28e | 120 | } |