1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/*******************************************************************************
* Goggles Audio Player Library *
********************************************************************************
* Copyright (C) 2010-2021 by Sander Jansen. All Rights Reserved *
* --- *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses. *
********************************************************************************/
#include "ap_defs.h"
#include "ap_buffer.h"
#include "ap_connect.h"
#include "ap_http.h"
#include "ap_input_plugin.h"
#include "ap_input_thread.h"
namespace ap {
class HttpInput : public InputPlugin {
protected:
HttpClient client;
FXlong content_position;
FXuint content_type;
FXint icy_interval;
FXint icy_count;
MemoryBuffer preview_buffer;
private:
HttpInput(const HttpInput&);
HttpInput &operator=(const HttpInput&);
protected:
void check_headers();
FXival icy_read(void*,FXival);
void icy_parse(const FXString&);
public:
/// Constructor
HttpInput(IOContext*);
FXbool open(const FXString & uri) override;
/// Read
FXival read(void*,FXival) override;
/// Preview
FXival preview(void*,FXival) override;
/// Set Position
FXlong position(FXlong offset,FXuint from) override;
/// Get Position
FXlong position() const override;
/// Size
FXlong size() override;
/// End of Input
FXbool eof() override;
/// Serial
FXbool serial() const override;
/// Get plugin type
FXuint plugin() const override;
/// Destructor
virtual ~HttpInput();
};
#define HTTP_TOKEN "[\\w!#$%&'*+-.^`|~]+"
#define HTTP_MEDIA_TYPE HTTP_TOKEN "/" HTTP_TOKEN
HttpInput::HttpInput(IOContext * ctx) : InputPlugin(ctx),
content_position(0),
content_type(Format::Unknown),
icy_interval(0),
icy_count(0) {
client.setConnectionFactory(new ThreadConnectionFactory(context));
}
HttpInput::~HttpInput() {
}
void HttpInput::check_headers() {
FXint p = client.headers.find("icy-metaint");
if (p!=-1) icy_count = icy_interval = client.headers.data(p).toInt();
HttpMediaType media;
if (client.getContentType(media)) {
content_type = ap_format_from_mime(media.mime);
}
}
FXbool HttpInput::open(const FXString & uri) {
FXString url = uri;
FXString headers = FXString::value("User-agent: gogglesmm/%d.%d\r\n"
"Icy-MetaData: 1\r\n"
"Accept: */*\r\n",0,1);
if (client.basic("GET",uri,headers)) {
if (client.status.code==HTTP_OK){
check_headers();
/// Try to guess content from uri.
if (content_type==Format::Unknown)
content_type=ap_format_from_extension(FXPath::extension(uri));
return true;
}
}
return false;
}
FXival HttpInput::preview(void*data,FXival count) {
FXival n;
preview_buffer.reserve(count);
if (client.getContentLength()>=0) {
if (content_position>=client.getContentLength())
return -1;
else
count=FXMIN((client.getContentLength()-content_position),count);
}
if (icy_interval)
n=icy_read(data,count);
else
n=client.readBody(data,count);
if (n>0)
preview_buffer.append(data,n);
return -1;
}
FXival HttpInput::read(void * data,FXival count) {
FXival n,t=0;
FXuchar * p = (FXuchar*)data;
/// Don't read past content
if (client.getContentLength()>=0) {
if (content_position>=client.getContentLength())
return 0;
else
count=FXMIN((client.getContentLength()-content_position),count);
}
// Read from preview buffer
if (preview_buffer.size()) {
n=preview_buffer.read(p,count);
if (0<n) { p+=n;count-=n; t+=n;}
}
// Regular Read
if (icy_interval)
n=icy_read(p,count);
else
n=client.readBody(p,count);
if (n>0) {
content_position+=n;
t+=n;
}
return t;
}
FXlong HttpInput::position(FXlong offset,FXuint from) {
if (from==FXIO::Current) {
FXuchar b;
for (FXlong i=0;i<offset;i++) {
if (read(&b,1)!=1)
return -1;
}
}
return -1;
//return client->position(offset,from);
}
FXlong HttpInput::position() const {
return content_position;
}
FXlong HttpInput::size() {
return client.getContentLength();
}
FXbool HttpInput::eof() {
if (client.getContentLength()>=0 && content_position>=client.getContentLength())
return true;
else if (preview_buffer.size())
return false;
else
return client.eof();
}
FXbool HttpInput::serial() const {
return true;
}
FXuint HttpInput::plugin() const {
return content_type;
}
void HttpInput::icy_parse(const FXString & str) {
FXString title = str.after('=').before(';');
if (title.length()) {
MetaInfo* meta = new MetaInfo();
meta->title = title;
context->post_meta(meta);
}
}
FXival HttpInput::icy_read(void*ptr,FXival count){
FXchar * out = static_cast<FXchar*>(ptr);
FXival nread=0,n=0;
if (icy_count<count) {
/// Read up to icy buffer
nread=client.readBody(out,icy_count);
if (__unlikely(nread!=icy_count)) {
if (nread>0) {
icy_count-=nread;
}
return nread;
}
// Adjust output
out+=nread;
count-=nread;
/// Read icy buffer size
FXuchar b=0;
n=client.readBody(&b,1);
if (__unlikely(n!=1)) return -1;
/// Read icy buffer
if (b) {
FXushort icy_size=((FXushort)b)*16;
FXString icy_buffer;
icy_buffer.length(icy_size);
n=client.readBody(&icy_buffer[0],icy_size);
if (__unlikely(n!=icy_size)) return -1;
icy_parse(icy_buffer);
}
/// reset icy count
icy_count=icy_interval;
/// Read remaining bytes
n=client.readBody(out,count);
if (__unlikely(n!=count)) return -1;
nread+=n;
icy_count-=n;
}
else {
nread=client.readBody(out,count);
if (__likely(nread>0)) {
icy_count-=nread;
}
}
return nread;
}
InputPlugin * ap_http_plugin(IOContext * context) {
return new HttpInput(context);
}
}
|