HttpByteStream.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: HttpByteStream.hpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #include "HttpByteStream.hpp" 00045 00046 HttpByteStream::HttpByteStream(const char* url, bool forReading) : ByteStream(url,forReading) { 00047 http = NULL; 00048 offset=0; 00049 } 00050 00051 00052 bool HttpByteStream::open() { 00053 if (http) { //already open 00054 dprintf_err("HttpByteStream::open: already connected to %s!\n",url); 00055 return true; 00056 } 00057 00058 if (readOnly) { 00059 http = new HttpRequest(url); 00060 if (!http->makeHttpRequest()) { 00061 dprintf_err("HttpByteStream::open: server/URI not found: %s!\n",url); 00062 return false; 00063 } else { 00064 dprintf_err("HttpByteStream::open: URI %s is open, mime-type: %s, %i content bytes will follow!\n", 00065 url,http->getMimeType(),http->getContentLength()); 00066 return true; 00067 } 00068 } 00069 else //not impl 00070 return false; 00071 } 00072 00073 00074 bool HttpByteStream::close() { 00075 if (!http) 00076 return false; 00077 else 00078 http->closeSock(); 00079 return true; 00080 } 00081 00082 00083 int HttpByteStream::getChar() { 00084 if (!http) 00085 return false; 00086 00087 char c; 00088 int ret = this->read(&c,1); 00089 if (ret == 1) 00090 return (int)c; 00091 else 00092 return ret; 00093 } 00094 00095 00096 int HttpByteStream::read(void *ptr, int size) { 00097 if (!http || !readOnly) 00098 return -1; 00099 int pos=0; 00100 int len=0; 00101 00102 do { 00103 len = http->continueReceive((char*)ptr+pos,size-pos); 00104 if (len>0) 00105 pos += len; 00106 } while ((len >= 0) && (pos<size)); 00107 00108 /* 00109 for (int i=0; i < size; i++) 00110 printf("%c",*((char*)ptr+i)); 00111 printf("%c\n"); 00112 00113 dprintf_full("HttpByteStream::read: wanted %i bytes, really got %i bytes, last return %i\n",size,pos,len); 00114 */ 00115 00116 offset+=pos; 00117 return pos; 00118 00119 } 00120 00121 00122 int HttpByteStream::write(void *ptr, int size){ 00123 if (!http|| readOnly) 00124 return -1; 00125 00126 /* NOT IMPLEMENTED */ 00127 return -1; 00128 } 00129 00130 00131 int HttpByteStream::seek(u64 pos) { 00132 if (!http) 00133 return -1; 00134 00135 return -1; 00136 } 00137 00138 00139 u64 HttpByteStream::tell() { 00140 if (!http) 00141 return 0; 00142 00143 return offset; 00144 }