~invernizzi/lightspark/readme-update

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
/**************************************************************************
    Lightspark, a free flash player implementation

    Copyright (C) 2009,2010  Alessandro Pignotti (a.pignotti@sssup.it)

    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 "netutils.h"
#include <curl/curl.h>
#include <string>
#include <iostream>

using namespace lightspark;

CurlDownloader::CurlDownloader(const tiny_string& u):buffer(NULL),len(0),tail(0),failed(false),waiting(false)
{
	//TODO: Url encode the string
	std::string tmp2;
	tmp2.reserve(u.len()*2);
	for(int i=0;i<u.len();i++)
	{
		if(u[i]==' ')
		{
			char buf[4];
			sprintf(buf,"%%%x",(unsigned char)u[i]);
			tmp2+=buf;
		}
		else
			tmp2.push_back(u[i]);
	}
	url=tmp2.c_str();

	sem_init(&available,0,0);
	sem_init(&mutex,0,1);
}

void CurlDownloader::execute()
{
	if(url.len()==0)
	{
		setFailed();
		return;
	}
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	if(curl)
	{
		std::cout << url << std::endl;
		curl_easy_setopt(curl, CURLOPT_URL, url.raw_buf());
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
		curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, write_header);
		curl_easy_setopt(curl, CURLOPT_HEADERDATA, this);
		res = curl_easy_perform(curl);
		if(res!=0)
			setFailed();
		curl_easy_cleanup(curl);
	}
	else
		setFailed();

	return;
}

bool CurlDownloader::download()
{
	execute();
	return !failed;
}

size_t CurlDownloader::write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
	CurlDownloader* th=static_cast<CurlDownloader*>(userp);
	sem_wait(&th->mutex);
	memcpy(th->buffer + th->tail,buffer,size*nmemb);
	th->tail+=(size*nmemb);
	if(th->waiting)
	{
		th->waiting=false;
		assert(size*nmemb);
		sem_post(&th->available);
	}
	else
		sem_post(&th->mutex);
	return size*nmemb;
}

size_t CurlDownloader::write_header(void *buffer, size_t size, size_t nmemb, void *userp)
{
	CurlDownloader* th=static_cast<CurlDownloader*>(userp);
	char* headerLine=(char*)buffer;

	std::cout << headerLine << std::endl;

	if(strncmp(headerLine,"HTTP/1.1 4",10)==0) //HTTP error, let's fail
		th->setFailed();
	else if(strncmp(headerLine,"Content-Length: ",16)==0)
	{
		//Now read the length and allocate the byteArray
		assert(th->buffer==NULL);
		th->len=atoi(headerLine+16);
		th->buffer=new uint8_t[th->len];
		th->setg((char*)th->buffer,(char*)th->buffer,(char*)th->buffer);
	}
	return size*nmemb;
}

void CurlDownloader::setFailed()
{
	sem_wait(&mutex);
	failed=true;
	if(waiting) //If we are waiting for some bytes, gives up and return EOF
		sem_post(&available);
	else
		sem_post(&mutex);
}

CurlDownloader::int_type CurlDownloader::underflow()
{
	sem_wait(&mutex);
	assert(gptr()==egptr());
	unsigned int firstIndex=tail;

	if((buffer+tail)==(uint8_t*)egptr()) //We have no more bytes
	{
		if(failed)
		{
			sem_post(&mutex);
			return -1;
		}
		else
		{
			waiting=true;
			sem_post(&mutex);
			sem_wait(&available);
		}
	}

	//The current pointer has to be saved
	char* cur=gptr();

	if(failed)
	{
		sem_post(&mutex);
		return -1;
	}

	//We have some bytes now, let's use them
	setg((char*)buffer,cur,(char*)buffer+tail);
	sem_post(&mutex);
	//Cast to signed, otherwise 0xff would become eof
	return (unsigned char)buffer[firstIndex];
}

CurlDownloader::pos_type CurlDownloader::seekpos(pos_type pos, std::ios_base::openmode mode)
{
	assert(mode==std::ios_base::in);
	sem_wait(&mutex);
	setg((char*)buffer,(char*)buffer+pos,(char*)buffer+tail);
	sem_post(&mutex);
	return pos;
}

CurlDownloader::pos_type CurlDownloader::seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode mode)
{
	assert(mode==std::ios_base::in);
	assert(off==0);
	pos_type ret=gptr()-eback();
	return ret;
}