1414#define HTTP_FAILED 0
1515#define HTTP_USER_AGENT "Mozilla/5.0 (PLAYSTATION 4; 1.00)"
1616
17+ static CURL * ftp_ctx = NULL ;
18+
1719
1820int http_init (void )
1921{
@@ -87,14 +89,12 @@ static void set_curl_opts(CURL* curl)
8789 }
8890}
8991
90- int http_download ( const char * url , const char * filename , const char * local_dst , int show_progress )
92+ static int _curl_download ( CURL * curl , const char * url , const char * filename , const char * local_dst , int show_progress )
9193{
9294 char full_url [1024 ];
93- CURL * curl ;
9495 CURLcode res ;
9596 FILE * fd ;
9697
97- curl = curl_easy_init ();
9898 if (!curl )
9999 {
100100 LOG ("ERROR: CURL INIT" );
@@ -104,15 +104,13 @@ int http_download(const char* url, const char* filename, const char* local_dst,
104104 fd = fopen (local_dst , "wb" );
105105 if (!fd ) {
106106 LOG ("fopen Error: File path '%s'" , local_dst );
107- curl_easy_cleanup (curl );
108107 return HTTP_FAILED ;
109108 }
110109
111110 if (!filename ) filename = "" ;
112111 snprintf (full_url , sizeof (full_url ), "%s%s" , url , filename );
113112 LOG ("URL: %s >> %s" , full_url , local_dst );
114113
115- set_curl_opts (curl );
116114 curl_easy_setopt (curl , CURLOPT_URL , full_url );
117115 // The function that will be used to write the data
118116 curl_easy_setopt (curl , CURLOPT_WRITEFUNCTION , fwrite );
@@ -139,8 +137,6 @@ int http_download(const char* url, const char* filename, const char* local_dst,
139137
140138 // close file descriptor
141139 fclose (fd );
142- // cleanup
143- curl_easy_cleanup (curl );
144140
145141 if (show_progress )
146142 end_progress_bar ();
@@ -155,6 +151,21 @@ int http_download(const char* url, const char* filename, const char* local_dst,
155151 return HTTP_SUCCESS ;
156152}
157153
154+ int http_download (const char * url , const char * filename , const char * local_dst , int show_progress )
155+ {
156+ int ret ;
157+ CURL * curl = curl_easy_init ();
158+
159+ if (!curl )
160+ return HTTP_FAILED ;
161+
162+ set_curl_opts (curl );
163+ ret = _curl_download (curl , url , filename , local_dst , show_progress );
164+ curl_easy_cleanup (curl );
165+
166+ return ret ;
167+ }
168+
158169void http_end (void )
159170{
160171 curl_global_cleanup ();
@@ -164,6 +175,32 @@ void http_end(void)
164175 sceSysmoduleUnloadModuleInternal (ORBIS_SYSMODULE_INTERNAL_NET );
165176}
166177
178+ void ftp_init (void )
179+ {
180+ if (!ftp_ctx && (ftp_ctx = curl_easy_init ()))
181+ set_curl_opts (ftp_ctx );
182+ }
183+
184+ void ftp_end (void )
185+ {
186+ if (ftp_ctx )
187+ curl_easy_cleanup (ftp_ctx );
188+
189+ ftp_ctx = NULL ;
190+ }
191+
192+ int ftp_download (const char * url , const char * filename , const char * local_dst , int show_progress )
193+ {
194+ if (!ftp_ctx )
195+ return HTTP_FAILED ;
196+
197+ curl_easy_setopt (ftp_ctx , CURLOPT_UPLOAD , 0 );
198+ curl_easy_setopt (ftp_ctx , CURLOPT_APPEND , 0 );
199+ curl_easy_setopt (ftp_ctx , CURLOPT_NOPROGRESS , 1L );
200+
201+ return _curl_download (ftp_ctx , url , filename , local_dst , show_progress );
202+ }
203+
167204int ftp_upload (const char * local_file , const char * url , const char * filename , int show_progress )
168205{
169206 FILE * fd ;
@@ -173,7 +210,7 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
173210 unsigned long fsize ;
174211
175212 /* get a curl handle */
176- curl = curl_easy_init () ;
213+ curl = ftp_ctx ;
177214 if (!curl )
178215 {
179216 LOG ("ERROR: CURL INIT" );
@@ -185,7 +222,6 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
185222 if (!fd )
186223 {
187224 LOG ("Couldn't open '%s'" , local_file );
188- curl_easy_cleanup (curl );
189225 return HTTP_FAILED ;
190226 }
191227
@@ -199,25 +235,19 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
199235 LOG ("Local file size: %lu bytes." , fsize );
200236 LOG ("Uploading (%s) -> (%s)" , local_file , remote_url );
201237
202- set_curl_opts (curl );
203238 /* enable uploading */
204239 curl_easy_setopt (curl , CURLOPT_UPLOAD , 1L );
205-
240+ curl_easy_setopt ( curl , CURLOPT_APPEND , 0L );
206241 /* specify target */
207242 curl_easy_setopt (curl , CURLOPT_URL , remote_url );
208-
209243 // create missing dirs if needed
210244 curl_easy_setopt (curl , CURLOPT_FTP_CREATE_MISSING_DIRS , CURLFTP_CREATE_DIR );
211-
212245 /* please ignore the IP in the PASV response */
213246 curl_easy_setopt (curl , CURLOPT_FTP_SKIP_PASV_IP , 1L );
214-
215247 /* we want to use our own read function */
216248 curl_easy_setopt (curl , CURLOPT_READFUNCTION , fread );
217-
218249 /* now specify which file to upload */
219250 curl_easy_setopt (curl , CURLOPT_READDATA , fd );
220-
221251 /* Set the size of the file to upload (optional). */
222252 curl_easy_setopt (curl , CURLOPT_INFILESIZE_LARGE , (curl_off_t )fsize );
223253
@@ -229,22 +259,18 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
229259 curl_easy_setopt (curl , CURLOPT_XFERINFODATA , (void * ) filename );
230260 curl_easy_setopt (curl , CURLOPT_NOPROGRESS , 0L );
231261 }
262+ else
263+ {
264+ curl_easy_setopt (curl , CURLOPT_NOPROGRESS , 1L );
265+ curl_easy_setopt (curl , CURLOPT_APPEND , 1L );
266+ }
232267
233268 /* Now run off and do what you have been told! */
234269 res = curl_easy_perform (curl );
235270
236- if (res == CURLE_SSL_CONNECT_ERROR )
237- {
238- curl_easy_setopt (curl , CURLOPT_USE_SSL , CURLUSESSL_NONE );
239- res = curl_easy_perform (curl );
240- }
241-
242271 /* close the local file */
243272 fclose (fd );
244273
245- /* always cleanup */
246- curl_easy_cleanup (curl );
247-
248274 if (show_progress )
249275 end_progress_bar ();
250276
0 commit comments