Dependencies: DMSupport DMemWin
embedded/HTTPFile.cpp@0:08606a13a816, 2016-06-02 (annotated)
- Committer:
- destinyXfate
- Date:
- Thu Jun 02 05:04:57 2016 +0000
- Revision:
- 0:08606a13a816
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
destinyXfate | 0:08606a13a816 | 1 | /* HTTPText.cpp */ |
destinyXfate | 0:08606a13a816 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
destinyXfate | 0:08606a13a816 | 3 | * |
destinyXfate | 0:08606a13a816 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
destinyXfate | 0:08606a13a816 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
destinyXfate | 0:08606a13a816 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
destinyXfate | 0:08606a13a816 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
destinyXfate | 0:08606a13a816 | 8 | * furnished to do so, subject to the following conditions: |
destinyXfate | 0:08606a13a816 | 9 | * |
destinyXfate | 0:08606a13a816 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
destinyXfate | 0:08606a13a816 | 11 | * substantial portions of the Software. |
destinyXfate | 0:08606a13a816 | 12 | * |
destinyXfate | 0:08606a13a816 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
destinyXfate | 0:08606a13a816 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
destinyXfate | 0:08606a13a816 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
destinyXfate | 0:08606a13a816 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
destinyXfate | 0:08606a13a816 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
destinyXfate | 0:08606a13a816 | 18 | */ |
destinyXfate | 0:08606a13a816 | 19 | |
destinyXfate | 0:08606a13a816 | 20 | #include "HTTPFile.h" |
destinyXfate | 0:08606a13a816 | 21 | |
destinyXfate | 0:08606a13a816 | 22 | #include <cstring> |
destinyXfate | 0:08606a13a816 | 23 | #include <stdio.h> |
destinyXfate | 0:08606a13a816 | 24 | |
destinyXfate | 0:08606a13a816 | 25 | #define OK 0 |
destinyXfate | 0:08606a13a816 | 26 | |
destinyXfate | 0:08606a13a816 | 27 | using std::memcpy; |
destinyXfate | 0:08606a13a816 | 28 | using std::strncpy; |
destinyXfate | 0:08606a13a816 | 29 | using std::strlen; |
destinyXfate | 0:08606a13a816 | 30 | |
destinyXfate | 0:08606a13a816 | 31 | #define MIN(x,y) (((x)<(y))?(x):(y)) |
destinyXfate | 0:08606a13a816 | 32 | |
destinyXfate | 0:08606a13a816 | 33 | HTTPFile::HTTPFile(FILE* pFile) : |
destinyXfate | 0:08606a13a816 | 34 | m_file(pFile), m_pos(0) { |
destinyXfate | 0:08606a13a816 | 35 | fseek(m_file, 0, SEEK_END); |
destinyXfate | 0:08606a13a816 | 36 | m_size = ftell(m_file); |
destinyXfate | 0:08606a13a816 | 37 | fseek(m_file, 0, SEEK_SET); |
destinyXfate | 0:08606a13a816 | 38 | } |
destinyXfate | 0:08606a13a816 | 39 | |
destinyXfate | 0:08606a13a816 | 40 | HTTPFile::HTTPFile(FILE* pFile, size_t size) : |
destinyXfate | 0:08606a13a816 | 41 | m_file(pFile), m_size(size), m_pos(0) { |
destinyXfate | 0:08606a13a816 | 42 | |
destinyXfate | 0:08606a13a816 | 43 | } |
destinyXfate | 0:08606a13a816 | 44 | |
destinyXfate | 0:08606a13a816 | 45 | //IHTTPDataIn |
destinyXfate | 0:08606a13a816 | 46 | /*virtual*/void HTTPFile::readReset() { |
destinyXfate | 0:08606a13a816 | 47 | m_pos = 0; |
destinyXfate | 0:08606a13a816 | 48 | } |
destinyXfate | 0:08606a13a816 | 49 | |
destinyXfate | 0:08606a13a816 | 50 | /*virtual*/int HTTPFile::read(char* buf, size_t len, size_t* pReadLen) { |
destinyXfate | 0:08606a13a816 | 51 | *pReadLen = MIN(len, m_size - m_pos); |
destinyXfate | 0:08606a13a816 | 52 | fread(buf, 1, *pReadLen, m_file); |
destinyXfate | 0:08606a13a816 | 53 | m_pos += *pReadLen; |
destinyXfate | 0:08606a13a816 | 54 | return OK; |
destinyXfate | 0:08606a13a816 | 55 | } |
destinyXfate | 0:08606a13a816 | 56 | |
destinyXfate | 0:08606a13a816 | 57 | /*virtual*/int HTTPFile::getDataType(char* type, size_t maxTypeLen) //Internet media type for Content-Type header |
destinyXfate | 0:08606a13a816 | 58 | { |
destinyXfate | 0:08606a13a816 | 59 | strncpy(type, "text/xml", maxTypeLen - 1); |
destinyXfate | 0:08606a13a816 | 60 | type[maxTypeLen - 1] = '\0'; |
destinyXfate | 0:08606a13a816 | 61 | return OK; |
destinyXfate | 0:08606a13a816 | 62 | } |
destinyXfate | 0:08606a13a816 | 63 | |
destinyXfate | 0:08606a13a816 | 64 | /*virtual*/bool HTTPFile::getIsChunked() //For Transfer-Encoding header |
destinyXfate | 0:08606a13a816 | 65 | { |
destinyXfate | 0:08606a13a816 | 66 | return false; |
destinyXfate | 0:08606a13a816 | 67 | } |
destinyXfate | 0:08606a13a816 | 68 | |
destinyXfate | 0:08606a13a816 | 69 | /*virtual*/size_t HTTPFile::getDataLen() |
destinyXfate | 0:08606a13a816 | 70 | //For Content-Length header |
destinyXfate | 0:08606a13a816 | 71 | { |
destinyXfate | 0:08606a13a816 | 72 | return m_size; |
destinyXfate | 0:08606a13a816 | 73 | } |
destinyXfate | 0:08606a13a816 | 74 | |
destinyXfate | 0:08606a13a816 | 75 | //IHTTPDataOut |
destinyXfate | 0:08606a13a816 | 76 | /*virtual*/void HTTPFile::writeReset() { |
destinyXfate | 0:08606a13a816 | 77 | m_pos = 0; |
destinyXfate | 0:08606a13a816 | 78 | } |
destinyXfate | 0:08606a13a816 | 79 | |
destinyXfate | 0:08606a13a816 | 80 | /*virtual*/int HTTPFile::write(const char* buf, size_t len) { |
destinyXfate | 0:08606a13a816 | 81 | // size_t writeLen = MIN(len, m_size - m_pos); |
destinyXfate | 0:08606a13a816 | 82 | // memcpy(m_pos, buf, writeLen); |
destinyXfate | 0:08606a13a816 | 83 | // m_pos += writeLen; |
destinyXfate | 0:08606a13a816 | 84 | // m_id[m_pos] = '\0'; |
destinyXfate | 0:08606a13a816 | 85 | return OK; |
destinyXfate | 0:08606a13a816 | 86 | } |
destinyXfate | 0:08606a13a816 | 87 | |
destinyXfate | 0:08606a13a816 | 88 | /*virtual*/void HTTPFile::setDataType(const char* type) //Internet media type from Content-Type header |
destinyXfate | 0:08606a13a816 | 89 | { |
destinyXfate | 0:08606a13a816 | 90 | |
destinyXfate | 0:08606a13a816 | 91 | } |
destinyXfate | 0:08606a13a816 | 92 | |
destinyXfate | 0:08606a13a816 | 93 | /*virtual*/void HTTPFile::setIsChunked(bool chunked) //From Transfer-Encoding header |
destinyXfate | 0:08606a13a816 | 94 | { |
destinyXfate | 0:08606a13a816 | 95 | |
destinyXfate | 0:08606a13a816 | 96 | } |
destinyXfate | 0:08606a13a816 | 97 | |
destinyXfate | 0:08606a13a816 | 98 | /*virtual*/void HTTPFile::setDataLen(size_t len) //From Content-Length header, or if the transfer is chunked, next chunk length |
destinyXfate | 0:08606a13a816 | 99 | { |
destinyXfate | 0:08606a13a816 | 100 | |
destinyXfate | 0:08606a13a816 | 101 | } |
destinyXfate | 0:08606a13a816 | 102 | |
destinyXfate | 0:08606a13a816 | 103 |