Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPFile.cpp Source File

HTTPFile.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "HTTPFile.h"
00025 
00026 //#define __DEBUG
00027 #include "dbg/dbg.h"
00028 
00029 HTTPFile::HTTPFile(const char* path) : HTTPData(), m_fp(NULL), m_path(path), m_len(0), m_chunked(false)
00030 {
00031 
00032 }
00033 
00034 HTTPFile::~HTTPFile()
00035 {
00036   closeFile();
00037 }
00038 
00039 void HTTPFile::clear()
00040 {
00041   closeFile();
00042   //Force reopening
00043 }
00044 
00045 int HTTPFile::read(char* buf, int len)
00046 {
00047   if(!openFile("r")) //File does not exist, or I/O error...
00048     return 0;
00049   len = fread(buf, 1, len, m_fp);
00050   if( feof(m_fp) )
00051   {
00052     //File read completely, we can close it
00053     closeFile();
00054   }
00055   return len;
00056 }
00057 
00058 int HTTPFile::write(const char* buf, int len)
00059 {
00060   if(!openFile("w")) //File does not exist, or I/O error...
00061     return 0;
00062   len = fwrite(buf, 1, len, m_fp);
00063   DBG("Written %d bytes in %d\n", len, m_fp);
00064   if( (!m_chunked && (ftell(m_fp) >= m_len)) ||
00065       (m_chunked && !len) )
00066   {
00067     //File received completely, we can close it
00068     closeFile();
00069   }
00070   return len;
00071 }
00072   
00073 string HTTPFile::getDataType() //Internet media type for Content-Type header
00074 {
00075   return ""; //Unknown
00076 }
00077 
00078 void HTTPFile::setDataType(const string& type) //Internet media type from Content-Type header
00079 {
00080   //Do not really care here
00081 }
00082 
00083 bool HTTPFile::getIsChunked() //For Transfer-Encoding header
00084 {
00085   return false;
00086 }
00087 
00088 void HTTPFile::setIsChunked(bool chunked) //From Transfer-Encoding header
00089 {
00090   m_chunked = chunked;
00091 }
00092   
00093 int HTTPFile::getDataLen() //For Content-Length header
00094 {
00095   return m_len;
00096 }
00097 
00098 void HTTPFile::setDataLen(int len) //From Content-Length header, or if the transfer is chunked, next chunk length
00099 {
00100   if(!m_chunked)
00101     m_len = len; //Useful so that we can close file when last byte is written
00102 }
00103 
00104 bool HTTPFile::openFile(const char* mode) //true on success, false otherwise
00105 {
00106   if(m_fp) 
00107     return true;
00108   
00109   m_fp = fopen(m_path.c_str(), mode);
00110   if(m_fp && mode[0]=='r')
00111   {
00112     //Seek EOF to get length
00113     fseek(m_fp, 0, SEEK_END);
00114     m_len = ftell(m_fp);
00115     fseek(m_fp, 0, SEEK_SET); //Goto SOF
00116   }
00117   
00118   DBG("fd = %d\n", m_fp);
00119   
00120   if(!m_fp) 
00121     return false;
00122     
00123   return true;
00124 }
00125 
00126 void HTTPFile::closeFile()
00127 {
00128   if(m_fp)
00129     fclose(m_fp);
00130 }