Replace cfile.c with connection.c

- cfile.h needed too many function calls and was too complex
 - connection.h does
     - only download in binary mode (json can handle that)
     - have no excess of getters and setters
     - allow to execute the whole request in a single function call
     - allow to be re-used for multiple requests
 - as a result, the code has 600 lines of code less
 - originally, connection.h was developed to use a global curl
   handle for all requests such that the same connection could be
   re-used. Unfortunately the MediaFire servers will close the
   connection after each request from their end:

      Bryan: "Unfortunately, we won't ever do keep-alive.  Closing the
      connection is a small part of a larger set of heuristics we have in
      place to prevent DOS/DDOS attacks."

   This causes massive performance impacts and those grow even larger when
   using SSL because the handshake has to be executed for every single
   request again.
This commit is contained in:
josch
2014-09-17 11:20:23 +02:00
parent fe42b6e668
commit ebedf596db
20 changed files with 346 additions and 974 deletions

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
@@ -31,20 +32,13 @@
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "cfile.h"
#include "strings.h"
#include "download.h"
void
_download_direct_cbio(cfile_t *cfile);
void
_download_direct_cbprogress(cfile_t *cfile);
#include "connection.h"
ssize_t
download_direct(file_t *file,char *local_dir)
download_direct(mfshell_t *mfshell, file_t *file, char *local_dir)
{
cfile_t *cfile;
const char *url;
const char *file_name;
char *file_path;
@@ -62,32 +56,14 @@ download_direct(file_t *file,char *local_dir)
if(file_name == NULL) return -1;
if(strlen(file_name) < 1) return -1;
// create the object as a sender
cfile = cfile_create();
// take the defaults but switch to binary mode
cfile_set_defaults(cfile);
cfile_set_mode(cfile,CFILE_MODE_BINARY);
cfile_set_url(cfile,url);
cfile_set_io_func(cfile,_download_direct_cbio);
cfile_set_progress_func(cfile,_download_direct_cbprogress);
if(local_dir[strlen(local_dir) - 1] == '/')
file_path = strdup_printf("%s%s",local_dir,file_name);
else
file_path = strdup_printf("%s/%s",local_dir,file_name);
cfile_set_userptr(cfile,(void*)file_path);
retval = cfile_exec(cfile);
cfile_destroy(cfile);
if(retval != CURLE_OK)
{
free(file_path);
return -1;
}
conn_t *conn = conn_create();
retval = conn_get_file(conn, url, file_path);
conn_destroy(conn);
/*
it is preferable to have the vfs tell us how many bytes the
@@ -104,50 +80,3 @@ download_direct(file_t *file,char *local_dir)
return bytes_read;
}
void
_download_direct_cbio(cfile_t *cfile)
{
FILE *file;
char *file_path;
size_t bytes_ready = 0;
const char *rx_buffer;
if(cfile == NULL) return;
file_path = (char*)cfile_get_userptr(cfile);
if(file_path == NULL) return;
bytes_ready = cfile_get_rx_buffer_size(cfile);
if(bytes_ready == 0) return;
file = fopen(file_path,"a+");
if(file != NULL)
{
rx_buffer = cfile_get_rx_buffer(cfile);
fwrite((const void*)rx_buffer,sizeof(char),bytes_ready,file);
}
fclose(file);
cfile_reset_rx_buffer(cfile);
return;
}
void
_download_direct_cbprogress(cfile_t *cfile)
{
double total;
double recv;
if(cfile == NULL) return;
total = cfile_get_rx_length(cfile);
recv = cfile_get_rx_count(cfile);
printf("\r %.0f / %.0f",recv,total);
return;
}