![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded C project:18/12/2014
Dependencies: DS1307 TextLCD mbed
NetServices/services/http/client/data/HTTPFile.cpp@0:8d87bc453349, 2014-12-18 (annotated)
- Committer:
- ninoderkinderen
- Date:
- Thu Dec 18 09:35:49 2014 +0000
- Revision:
- 0:8d87bc453349
Programma embedded C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ninoderkinderen | 0:8d87bc453349 | 1 | |
ninoderkinderen | 0:8d87bc453349 | 2 | /* |
ninoderkinderen | 0:8d87bc453349 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
ninoderkinderen | 0:8d87bc453349 | 4 | |
ninoderkinderen | 0:8d87bc453349 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
ninoderkinderen | 0:8d87bc453349 | 6 | of this software and associated documentation files (the "Software"), to deal |
ninoderkinderen | 0:8d87bc453349 | 7 | in the Software without restriction, including without limitation the rights |
ninoderkinderen | 0:8d87bc453349 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
ninoderkinderen | 0:8d87bc453349 | 9 | copies of the Software, and to permit persons to whom the Software is |
ninoderkinderen | 0:8d87bc453349 | 10 | furnished to do so, subject to the following conditions: |
ninoderkinderen | 0:8d87bc453349 | 11 | |
ninoderkinderen | 0:8d87bc453349 | 12 | The above copyright notice and this permission notice shall be included in |
ninoderkinderen | 0:8d87bc453349 | 13 | all copies or substantial portions of the Software. |
ninoderkinderen | 0:8d87bc453349 | 14 | |
ninoderkinderen | 0:8d87bc453349 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ninoderkinderen | 0:8d87bc453349 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ninoderkinderen | 0:8d87bc453349 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ninoderkinderen | 0:8d87bc453349 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ninoderkinderen | 0:8d87bc453349 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ninoderkinderen | 0:8d87bc453349 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
ninoderkinderen | 0:8d87bc453349 | 21 | THE SOFTWARE. |
ninoderkinderen | 0:8d87bc453349 | 22 | */ |
ninoderkinderen | 0:8d87bc453349 | 23 | |
ninoderkinderen | 0:8d87bc453349 | 24 | #include "HTTPFile.h" |
ninoderkinderen | 0:8d87bc453349 | 25 | |
ninoderkinderen | 0:8d87bc453349 | 26 | //#define __DEBUG |
ninoderkinderen | 0:8d87bc453349 | 27 | #include "dbg/dbg.h" |
ninoderkinderen | 0:8d87bc453349 | 28 | |
ninoderkinderen | 0:8d87bc453349 | 29 | HTTPFile::HTTPFile(const char* path) : HTTPData(), m_fp(NULL), m_path(path), m_len(0), m_chunked(false) |
ninoderkinderen | 0:8d87bc453349 | 30 | { |
ninoderkinderen | 0:8d87bc453349 | 31 | |
ninoderkinderen | 0:8d87bc453349 | 32 | } |
ninoderkinderen | 0:8d87bc453349 | 33 | |
ninoderkinderen | 0:8d87bc453349 | 34 | HTTPFile::~HTTPFile() |
ninoderkinderen | 0:8d87bc453349 | 35 | { |
ninoderkinderen | 0:8d87bc453349 | 36 | closeFile(); |
ninoderkinderen | 0:8d87bc453349 | 37 | } |
ninoderkinderen | 0:8d87bc453349 | 38 | |
ninoderkinderen | 0:8d87bc453349 | 39 | void HTTPFile::clear() |
ninoderkinderen | 0:8d87bc453349 | 40 | { |
ninoderkinderen | 0:8d87bc453349 | 41 | closeFile(); |
ninoderkinderen | 0:8d87bc453349 | 42 | //Force reopening |
ninoderkinderen | 0:8d87bc453349 | 43 | } |
ninoderkinderen | 0:8d87bc453349 | 44 | |
ninoderkinderen | 0:8d87bc453349 | 45 | int HTTPFile::read(char* buf, int len) |
ninoderkinderen | 0:8d87bc453349 | 46 | { |
ninoderkinderen | 0:8d87bc453349 | 47 | if(!openFile("r")) //File does not exist, or I/O error... |
ninoderkinderen | 0:8d87bc453349 | 48 | return 0; |
ninoderkinderen | 0:8d87bc453349 | 49 | len = fread(buf, 1, len, m_fp); |
ninoderkinderen | 0:8d87bc453349 | 50 | if( feof(m_fp) ) |
ninoderkinderen | 0:8d87bc453349 | 51 | { |
ninoderkinderen | 0:8d87bc453349 | 52 | //File read completely, we can close it |
ninoderkinderen | 0:8d87bc453349 | 53 | closeFile(); |
ninoderkinderen | 0:8d87bc453349 | 54 | } |
ninoderkinderen | 0:8d87bc453349 | 55 | return len; |
ninoderkinderen | 0:8d87bc453349 | 56 | } |
ninoderkinderen | 0:8d87bc453349 | 57 | |
ninoderkinderen | 0:8d87bc453349 | 58 | int HTTPFile::write(const char* buf, int len) |
ninoderkinderen | 0:8d87bc453349 | 59 | { |
ninoderkinderen | 0:8d87bc453349 | 60 | if(!openFile("w")) //File does not exist, or I/O error... |
ninoderkinderen | 0:8d87bc453349 | 61 | return 0; |
ninoderkinderen | 0:8d87bc453349 | 62 | len = fwrite(buf, 1, len, m_fp); |
ninoderkinderen | 0:8d87bc453349 | 63 | DBG("Written %d bytes in %d\n", len, m_fp); |
ninoderkinderen | 0:8d87bc453349 | 64 | if( (!m_chunked && (ftell(m_fp) >= m_len)) || |
ninoderkinderen | 0:8d87bc453349 | 65 | (m_chunked && !len) ) |
ninoderkinderen | 0:8d87bc453349 | 66 | { |
ninoderkinderen | 0:8d87bc453349 | 67 | //File received completely, we can close it |
ninoderkinderen | 0:8d87bc453349 | 68 | closeFile(); |
ninoderkinderen | 0:8d87bc453349 | 69 | } |
ninoderkinderen | 0:8d87bc453349 | 70 | return len; |
ninoderkinderen | 0:8d87bc453349 | 71 | } |
ninoderkinderen | 0:8d87bc453349 | 72 | |
ninoderkinderen | 0:8d87bc453349 | 73 | string HTTPFile::getDataType() //Internet media type for Content-Type header |
ninoderkinderen | 0:8d87bc453349 | 74 | { |
ninoderkinderen | 0:8d87bc453349 | 75 | return ""; //Unknown |
ninoderkinderen | 0:8d87bc453349 | 76 | } |
ninoderkinderen | 0:8d87bc453349 | 77 | |
ninoderkinderen | 0:8d87bc453349 | 78 | void HTTPFile::setDataType(const string& type) //Internet media type from Content-Type header |
ninoderkinderen | 0:8d87bc453349 | 79 | { |
ninoderkinderen | 0:8d87bc453349 | 80 | //Do not really care here |
ninoderkinderen | 0:8d87bc453349 | 81 | } |
ninoderkinderen | 0:8d87bc453349 | 82 | |
ninoderkinderen | 0:8d87bc453349 | 83 | bool HTTPFile::getIsChunked() //For Transfer-Encoding header |
ninoderkinderen | 0:8d87bc453349 | 84 | { |
ninoderkinderen | 0:8d87bc453349 | 85 | return false; |
ninoderkinderen | 0:8d87bc453349 | 86 | } |
ninoderkinderen | 0:8d87bc453349 | 87 | |
ninoderkinderen | 0:8d87bc453349 | 88 | void HTTPFile::setIsChunked(bool chunked) //From Transfer-Encoding header |
ninoderkinderen | 0:8d87bc453349 | 89 | { |
ninoderkinderen | 0:8d87bc453349 | 90 | m_chunked = chunked; |
ninoderkinderen | 0:8d87bc453349 | 91 | } |
ninoderkinderen | 0:8d87bc453349 | 92 | |
ninoderkinderen | 0:8d87bc453349 | 93 | int HTTPFile::getDataLen() //For Content-Length header |
ninoderkinderen | 0:8d87bc453349 | 94 | { |
ninoderkinderen | 0:8d87bc453349 | 95 | return m_len; |
ninoderkinderen | 0:8d87bc453349 | 96 | } |
ninoderkinderen | 0:8d87bc453349 | 97 | |
ninoderkinderen | 0:8d87bc453349 | 98 | void HTTPFile::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length |
ninoderkinderen | 0:8d87bc453349 | 99 | { |
ninoderkinderen | 0:8d87bc453349 | 100 | if(!m_chunked) |
ninoderkinderen | 0:8d87bc453349 | 101 | m_len = len; //Useful so that we can close file when last byte is written |
ninoderkinderen | 0:8d87bc453349 | 102 | } |
ninoderkinderen | 0:8d87bc453349 | 103 | |
ninoderkinderen | 0:8d87bc453349 | 104 | bool HTTPFile::openFile(const char* mode) //true on success, false otherwise |
ninoderkinderen | 0:8d87bc453349 | 105 | { |
ninoderkinderen | 0:8d87bc453349 | 106 | if(m_fp) |
ninoderkinderen | 0:8d87bc453349 | 107 | return true; |
ninoderkinderen | 0:8d87bc453349 | 108 | |
ninoderkinderen | 0:8d87bc453349 | 109 | m_fp = fopen(m_path.c_str(), mode); |
ninoderkinderen | 0:8d87bc453349 | 110 | if(m_fp && mode[0]=='r') |
ninoderkinderen | 0:8d87bc453349 | 111 | { |
ninoderkinderen | 0:8d87bc453349 | 112 | //Seek EOF to get length |
ninoderkinderen | 0:8d87bc453349 | 113 | fseek(m_fp, 0, SEEK_END); |
ninoderkinderen | 0:8d87bc453349 | 114 | m_len = ftell(m_fp); |
ninoderkinderen | 0:8d87bc453349 | 115 | fseek(m_fp, 0, SEEK_SET); //Goto SOF |
ninoderkinderen | 0:8d87bc453349 | 116 | } |
ninoderkinderen | 0:8d87bc453349 | 117 | |
ninoderkinderen | 0:8d87bc453349 | 118 | DBG("fd = %d\n", m_fp); |
ninoderkinderen | 0:8d87bc453349 | 119 | |
ninoderkinderen | 0:8d87bc453349 | 120 | if(!m_fp) |
ninoderkinderen | 0:8d87bc453349 | 121 | return false; |
ninoderkinderen | 0:8d87bc453349 | 122 | |
ninoderkinderen | 0:8d87bc453349 | 123 | return true; |
ninoderkinderen | 0:8d87bc453349 | 124 | } |
ninoderkinderen | 0:8d87bc453349 | 125 | |
ninoderkinderen | 0:8d87bc453349 | 126 | void HTTPFile::closeFile() |
ninoderkinderen | 0:8d87bc453349 | 127 | { |
ninoderkinderen | 0:8d87bc453349 | 128 | if(m_fp) |
ninoderkinderen | 0:8d87bc453349 | 129 | fclose(m_fp); |
ninoderkinderen | 0:8d87bc453349 | 130 | } |