2005-02-28 07:16:19 +00:00
# include "quakedef.h"
2004-08-23 01:40:25 +00:00
# include "iweb.h"
2005-11-29 13:14:15 +00:00
# include "netinc.h"
2013-06-23 02:17:02 +00:00
# include "fs.h"
2004-08-23 01:40:25 +00:00
2016-07-15 12:26:24 +00:00
# if defined(WEBCLIENT)
2016-07-12 00:40:13 +00:00
# ifndef NPFTE
static struct dl_download * activedownloads ;
# endif
2013-04-04 08:08:49 +00:00
# if defined(FTE_TARGET_WEB)
2013-05-14 18:38:42 +00:00
# define MYJS 1
# if MYJS
# include "web/ftejslib.h"
# else
2013-04-04 08:08:49 +00:00
# include <emscripten/emscripten.h>
2013-05-14 18:38:42 +00:00
# endif
2013-04-04 08:08:49 +00:00
2014-08-25 07:35:41 +00:00
static void DL_Cancel ( struct dl_download * dl )
2013-04-04 08:08:49 +00:00
{
2014-08-25 07:35:41 +00:00
//FIXME: clear out the callbacks somehow
2013-04-04 08:08:49 +00:00
dl - > ctx = NULL ;
}
static void DL_OnLoad ( void * c , void * data , int datasize )
{
struct dl_download * dl = c ;
//make sure the file is 'open'.
if ( ! dl - > file )
{
if ( * dl - > localname )
{
2014-04-27 23:16:07 +00:00
FS_CreatePath ( dl - > localname , FS_GAMEONLY ) ;
dl - > file = FS_OpenVFS ( dl - > localname , " w+b " , FS_GAMEONLY ) ;
2013-04-04 08:08:49 +00:00
}
else
{
//emscripten does not close the file. plus we seem to end up with infinite loops.
2013-08-07 14:20:24 +00:00
dl - > file = FS_OpenTemp ( ) ;
2013-04-04 08:08:49 +00:00
}
}
if ( dl - > file )
{
VFS_WRITE ( dl - > file , data , datasize ) ;
2013-08-07 14:20:24 +00:00
VFS_SEEK ( dl - > file , 0 ) ;
dl - > status = DL_FINISHED ;
2013-04-04 08:08:49 +00:00
}
2013-08-07 14:20:24 +00:00
else
dl - > status = DL_FAILED ;
2013-04-04 08:08:49 +00:00
dl - > replycode = 200 ;
2013-05-14 18:38:42 +00:00
# if !MYJS
2013-04-04 08:08:49 +00:00
dl - > completed + = datasize ;
2013-05-14 18:38:42 +00:00
# endif
2013-04-04 08:08:49 +00:00
}
2013-05-14 18:38:42 +00:00
# if MYJS
static void DL_OnError ( void * c , int ecode )
# else
2013-04-04 08:08:49 +00:00
static void DL_OnError ( void * c )
2013-05-14 18:38:42 +00:00
# endif
2013-04-04 08:08:49 +00:00
{
struct dl_download * dl = c ;
2013-05-14 18:38:42 +00:00
# if MYJS
dl - > replycode = ecode ;
# else
2013-04-04 08:08:49 +00:00
dl - > replycode = 404 ; //we don't actually know. should we not do this?
2013-05-14 18:38:42 +00:00
# endif
2015-08-20 03:17:47 +00:00
Con_Printf ( " download: %s: error %i \n " , dl - > url , dl - > replycode ) ;
2013-04-04 08:08:49 +00:00
dl - > status = DL_FAILED ;
}
2013-05-14 18:38:42 +00:00
static void DL_OnProgress ( void * c , int position , int totalsize )
{
struct dl_download * dl = c ;
2014-08-25 07:35:41 +00:00
2013-05-14 18:38:42 +00:00
dl - > completed = position ;
dl - > totalsize = totalsize ;
}
2013-04-04 08:08:49 +00:00
//this becomes a poll function. the main thread will call this once a frame or so.
2013-06-23 20:11:58 +00:00
qboolean DL_Decide ( struct dl_download * dl )
2013-04-04 08:08:49 +00:00
{
const char * url = dl - > redir ;
if ( ! * url )
url = dl - > url ;
2013-10-08 14:28:11 +00:00
if ( dl - > postdata )
{
2014-08-25 07:35:41 +00:00
DL_Cancel ( dl ) ;
2013-10-08 14:28:11 +00:00
return false ; //safe to destroy it now
}
2013-04-04 08:08:49 +00:00
if ( dl - > ctx )
{
2014-08-25 07:35:41 +00:00
if ( dl - > status = = DL_FINISHED )
return false ;
if ( dl - > status = = DL_FAILED )
2013-04-04 08:08:49 +00:00
{
2014-08-25 07:35:41 +00:00
DL_Cancel ( dl ) ;
return false ;
2013-04-04 08:08:49 +00:00
}
}
else
{
dl - > status = DL_ACTIVE ;
2014-08-25 07:35:41 +00:00
dl - > abort = DL_Cancel ;
2013-04-04 08:08:49 +00:00
dl - > ctx = dl ;
2013-05-14 18:38:42 +00:00
# if MYJS
emscriptenfte_async_wget_data2 ( url , dl , DL_OnLoad , DL_OnError , DL_OnProgress ) ;
# else
//annoyingly, emscripten doesn't provide an onprogress callback, unlike firefox etc, so we can't actually tell how far its got.
//we'd need to provide our own js library to fix this. it can be done, I'm just lazy.
2013-04-04 08:08:49 +00:00
emscripten_async_wget_data ( url , dl , DL_OnLoad , DL_OnError ) ;
2013-05-14 18:38:42 +00:00
# endif
2013-04-04 08:08:49 +00:00
}
return true ;
}
# elif defined(NACL)
2013-03-12 23:09:25 +00:00
# include <ppapi/c/pp_errors.h>
# include <ppapi/c/ppb_core.h>
# include <ppapi/c/pp_file_info.h>
# include <ppapi/c/ppb_file_system.h>
# include <ppapi/c/ppb_file_ref.h>
# include <ppapi/c/ppb_url_request_info.h>
# include <ppapi/c/ppb_url_response_info.h>
# include <ppapi/c/pp_var.h>
# include <ppapi/c/ppb_var.h>
# include <ppapi/c/ppb_file_io.h>
# include <ppapi/c/ppb_url_loader.h>
extern PPB_Core * ppb_core ;
extern PPB_URLRequestInfo * urlrequestinfo ;
extern PPB_URLLoader * urlloader ;
extern PP_Instance pp_instance ;
extern PPB_Var * ppb_var_interface ;
struct nacl_dl {
char buffer [ 65536 ] ;
PP_Resource req ;
} ;
static void readfinished ( void * user_data , int32_t result )
{
struct dl_download * f = user_data ;
struct nacl_dl * ctx = f - > ctx ;
struct PP_CompletionCallback ccb = { readfinished , f , PP_COMPLETIONCALLBACK_FLAG_NONE } ;
//trying to clean up
if ( ! ctx )
return ;
// Sys_Printf("lastresult: %i\n", result);
if ( result = = PP_OK )
{
// Sys_Printf("%s completed\n", f->url);
ppb_core - > ReleaseResource ( ctx - > req ) ;
ctx - > req = 0 ;
f - > status = DL_FINISHED ;
return ;
}
for ( ; result > 0 ; result = urlloader - > ReadResponseBody ( ctx - > req , ctx - > buffer , sizeof ( ctx - > buffer ) , ccb ) )
{
//make sure the file is 'open'.
if ( ! f - > file )
{
if ( * f - > localname )
{
FS_CreatePath ( f - > localname , FS_GAME ) ;
f - > file = FS_OpenVFS ( f - > localname , " w+b " , FS_GAME ) ;
}
else
f - > file = FS_OpenTemp ( ) ;
}
// Sys_Printf("write: %i\n", result);
VFS_WRITE ( f - > file , ctx - > buffer , result ) ;
f - > completed + = result ;
}
// Sys_Printf("result: %i\n", result);
if ( result ! = PP_OK_COMPLETIONPENDING )
{
Sys_Printf ( " file %s failed or something \n " , f - > url ) ;
ppb_core - > ReleaseResource ( ctx - > req ) ;
ctx - > req = 0 ;
f - > status = DL_FAILED ;
}
}
//urloader->open completed
static void dlstarted ( void * user_data , int32_t result )
{
struct dl_download * f = user_data ;
struct nacl_dl * ctx = f - > ctx ;
struct PP_CompletionCallback ccb = { readfinished , f , PP_COMPLETIONCALLBACK_FLAG_NONE } ;
readfinished ( user_data , urlloader - > ReadResponseBody ( ctx - > req , ctx - > buffer , sizeof ( ctx - > buffer ) , ccb ) ) ;
}
2013-11-06 21:55:56 +00:00
static void nadl_cleanup_cb ( void * user_data , int32_t result )
2013-03-12 23:09:25 +00:00
{
struct nacl_dl * ctx = user_data ;
if ( ctx - > req )
ppb_core - > ReleaseResource ( ctx - > req ) ;
free ( ctx ) ;
}
void NADL_Cleanup ( struct dl_download * dl )
{
struct nacl_dl * ctx = dl - > ctx ;
//we can't free the ctx memory etc, in case the browser still has requests pending on it before it handles our close.
//so set up a callback to do it later
dl - > ctx = NULL ; //orphan
2013-11-06 21:55:56 +00:00
struct PP_CompletionCallback ccb = { nadl_cleanup_cb , ctx , PP_COMPLETIONCALLBACK_FLAG_NONE } ;
2013-03-12 23:09:25 +00:00
ppb_core - > CallOnMainThread ( 1000 , ccb , 0 ) ;
}
2013-06-23 02:17:02 +00:00
qboolean DL_Decide ( struct dl_download * dl )
2013-03-12 23:09:25 +00:00
{
const char * url = dl - > redir ;
struct nacl_dl * ctx ;
if ( ! * url )
url = dl - > url ;
2013-10-08 14:28:11 +00:00
if ( dl - > postdata )
{
2013-11-06 21:55:56 +00:00
NADL_Cleanup ( dl ) ;
2013-10-08 14:28:11 +00:00
return false ; //safe to destroy it now
}
2013-03-12 23:09:25 +00:00
if ( dl - > ctx )
{
if ( dl - > status = = DL_FAILED | | dl - > status = = DL_FINISHED )
{
NADL_Cleanup ( dl ) ;
return false ; //safe to destroy it now
}
}
else
{
PP_Resource dlri ;
dl - > status = DL_ACTIVE ;
dl - > abort = NADL_Cleanup ;
dl - > ctx = ctx = Z_Malloc ( sizeof ( * ctx ) ) ;
/*everything goes via nacl, so we might as well just init that here*/
ctx - > req = urlloader - > Create ( pp_instance ) ;
dlri = urlrequestinfo - > Create ( pp_instance ) ;
urlrequestinfo - > SetProperty ( dlri , PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS , ppb_var_interface - > VarFromUtf8 ( url , strlen ( url ) ) ) ;
urlrequestinfo - > SetProperty ( dlri , PP_URLREQUESTPROPERTY_URL , ppb_var_interface - > VarFromUtf8 ( url , strlen ( url ) ) ) ;
struct PP_CompletionCallback ccb = { dlstarted , dl , PP_COMPLETIONCALLBACK_FLAG_NONE } ;
urlloader - > Open ( ctx - > req , dlri , ccb ) ;
ppb_core - > ReleaseResource ( dlri ) ;
}
2010-03-14 14:35:56 +00:00
2013-03-12 23:09:25 +00:00
return true ;
}
# else
2013-06-23 02:17:02 +00:00
qboolean DL_Decide ( struct dl_download * dl ) ;
2010-05-01 22:47:47 +00:00
2004-08-23 01:40:25 +00:00
/*
This file does one thing . Connects to servers and grabs the specified file . It doesn ' t do any uploading whatsoever . Live with it .
It doesn ' t use persistant connections .
*/
2010-03-14 14:35:56 +00:00
2015-08-24 21:44:22 +00:00
# define COOKIECOOKIECOOKIE
2016-02-10 23:23:43 +00:00
# ifdef COOKIECOOKIECOOKIE
2015-08-24 21:44:22 +00:00
typedef struct cookie_s
{
struct cookie_s * next ;
char * domain ;
int secure ;
char * name ;
char * value ;
} cookie_t ;
cookie_t * cookies ;
2016-07-17 18:41:01 +00:00
# ifdef NPFTE
# define Z_Malloc malloc
# define Z_Free free
# endif
2015-08-24 21:44:22 +00:00
//set a specific cookie.
void Cookie_Feed ( char * domain , int secure , char * name , char * value )
{
cookie_t * * link , * c ;
2016-02-10 23:23:43 +00:00
Sys_LockMutex ( com_resourcemutex ) ;
2016-02-15 06:01:17 +00:00
for ( link = & cookies ; ( c = * link ) ! = NULL ; link = & ( * link ) - > next )
2015-08-24 21:44:22 +00:00
{
if ( ! strcmp ( c - > domain , domain ) & & c - > secure = = secure & & ! strcmp ( c - > name , name ) )
break ;
}
//delete it, if it exists, so we can create it anew.
if ( c )
{
* link = c - > next ;
Z_Free ( c ) ;
}
2016-02-10 23:23:43 +00:00
if ( value & & * value )
2015-08-24 21:44:22 +00:00
{
2016-02-10 23:23:43 +00:00
// Con_Printf("Setting cookie http%s://%s/ %s=%s\n", secure?"s":"", domain, name, value);
c = Z_Malloc ( sizeof ( * c ) + strlen ( domain ) + strlen ( name ) + strlen ( value ) + 3 ) ;
c - > domain = ( char * ) ( c + 1 ) ;
strcpy ( c - > domain , domain ) ;
c - > secure = secure ;
c - > name = c - > domain + strlen ( c - > domain ) + 1 ;
strcpy ( c - > name , name ) ;
c - > value = c - > name + strlen ( c - > name ) + 1 ;
strcpy ( c - > value , value ) ;
c - > next = cookies ;
cookies = c ;
2015-08-24 21:44:22 +00:00
}
2016-02-10 23:23:43 +00:00
else
{
// Con_Printf("Deleted cookie http%s://%s/ %s\n", secure?"s":"", domain, name);
}
Sys_UnlockMutex ( com_resourcemutex ) ;
2015-08-24 21:44:22 +00:00
}
//just removes all the cookies it can.
void Cookie_Monster ( void )
{
cookie_t * c ;
while ( cookies )
{
c = cookies ;
cookies = c - > next ;
Z_Free ( c ) ;
}
}
//parses Set-Cookie: THISPARTONLY\r\n
//we don't support:
//domain) we don't have a list of composite roots, like .co.uk, and thus this wouldn't work very safely anyway. thus we require the exact same host each time
//path) I'm going to call this an optimisation feature and not bother with it... hopefully there won't be too many sites that have sub-paths or third-party stuff... gah.
//httponly) irrelevant until we support javascript... which we don't.
//secure) assumed to be true. https:// vs http:// are thus completely independant. sorry.
//expires) gah, parsing time values sucks! plus we don't have persistent storage.
void Cookie_Parse ( char * domain , int secure , char * line , char * end )
{
char * e ;
while ( * line = = ' ' & & line < end )
line + + ;
for ( e = line ; e < end ; e + + )
if ( * e = = ' ; ' )
end = e ;
for ( e = line ; e < end ; e + + )
if ( * e = = ' = ' )
break ;
* e = 0 ;
* end = 0 ;
Cookie_Feed ( domain , secure , line , e + 1 ) ;
}
2016-02-10 23:23:43 +00:00
//outputs a complete http line: Cookie: a=v1; b=v2\r\n
2015-08-24 21:44:22 +00:00
void Cookie_Regurgitate ( char * domain , int secure , char * buffer , size_t buffersize )
{
qboolean hascookies = false ;
cookie_t * c ;
2016-02-15 06:01:17 +00:00
// char *l = buffer;
2015-08-24 21:44:22 +00:00
buffersize - = 3 ; //\r\n\0
* buffer = 0 ;
2016-02-10 23:23:43 +00:00
Sys_LockMutex ( com_resourcemutex ) ;
2015-08-24 21:44:22 +00:00
for ( c = cookies ; c ; c = c - > next )
{
if ( ! strcmp ( c - > domain , domain ) & & c - > secure = = secure )
{
int nlen , vlen ;
if ( ! hascookies )
{
if ( buffersize < 8 )
break ;
strcpy ( buffer , " Cookie: " ) ;
buffersize - = 8 ;
hascookies = true ;
}
else
{
if ( buffersize < 2 )
break ;
strcpy ( buffer , " ; " ) ;
buffersize - = 2 ;
}
buffer + = strlen ( buffer ) ;
nlen = strlen ( c - > name ) ;
vlen = strlen ( c - > value ) ;
if ( buffersize < nlen + 1 + vlen )
break ;
memcpy ( buffer , c - > name , nlen ) ;
buffer + = nlen ;
* buffer + + = ' = ' ;
memcpy ( buffer , c - > value , vlen ) ;
buffer + = vlen ;
}
}
2016-02-10 23:23:43 +00:00
Sys_UnlockMutex ( com_resourcemutex ) ;
2015-08-24 21:44:22 +00:00
if ( hascookies )
strcpy ( buffer , " \r \n " ) ;
else
* buffer = 0 ;
// if (*l)
// Con_Printf("Sending cookie(s) to http%s://%s/ %s\n", secure?"s":"", domain, l);
}
2016-02-10 23:23:43 +00:00
# endif
2015-08-24 21:44:22 +00:00
2010-03-14 14:35:56 +00:00
struct http_dl_ctx_s {
2014-12-31 06:00:55 +00:00
// struct dl_download *dlctx;
2010-03-14 14:35:56 +00:00
2015-01-02 05:57:14 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
vfsfile_t * stream ;
SOCKET sock ; //so we can wait on it when multithreaded.
2014-12-25 20:10:05 +00:00
# else
2013-10-08 14:28:11 +00:00
SOCKET sock ; //FIXME: support https.
2014-12-25 20:10:05 +00:00
# endif
2010-03-14 14:35:56 +00:00
char * buffer ;
2015-08-24 21:44:22 +00:00
char server [ 128 ] ;
qboolean secure ;
2015-04-14 23:12:17 +00:00
size_t bufferused ;
size_t bufferlen ;
2010-03-14 14:35:56 +00:00
2015-04-14 23:12:17 +00:00
size_t totalreceived ; //useful when we're just dumping to a file.
2010-03-14 14:35:56 +00:00
2013-06-23 02:17:02 +00:00
struct vfsfile_s * file ; //if gzipping, this is a temporary file. we'll write to the real file from this after the transfer is complete.
qboolean gzip ;
2010-03-14 14:35:56 +00:00
qboolean chunking ;
2015-04-14 23:12:17 +00:00
size_t chunksize ;
size_t chunked ;
2010-03-14 14:35:56 +00:00
2013-06-23 03:59:48 +00:00
enum { HC_REQUESTING , HC_GETTINGHEADER , HC_GETTING } state ;
2010-03-14 14:35:56 +00:00
2015-04-14 23:12:17 +00:00
size_t contentlength ;
2010-03-14 14:35:56 +00:00
} ;
void HTTP_Cleanup ( struct dl_download * dl )
{
struct http_dl_ctx_s * con = dl - > ctx ;
dl - > ctx = NULL ;
2015-01-02 05:57:14 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
if ( con - > stream )
VFS_CLOSE ( con - > stream ) ;
con - > stream = NULL ;
2014-12-25 20:10:05 +00:00
# else
2010-03-14 14:35:56 +00:00
if ( con - > sock ! = INVALID_SOCKET )
closesocket ( con - > sock ) ;
con - > sock = INVALID_SOCKET ;
2014-12-25 20:10:05 +00:00
# endif
2010-03-14 14:35:56 +00:00
free ( con - > buffer ) ;
free ( con ) ;
2012-11-27 03:23:19 +00:00
dl - > abort = NULL ;
2010-03-14 14:35:56 +00:00
dl - > status = DL_PENDING ;
dl - > completed = 0 ;
dl - > totalsize = 0 ;
}
static void ExpandBuffer ( struct http_dl_ctx_s * con , int quant )
{
int newlen ;
newlen = con - > bufferlen + quant ;
con - > buffer = realloc ( con - > buffer , newlen ) ;
con - > bufferlen = newlen ;
}
static qboolean HTTP_DL_Work ( struct dl_download * dl )
{
struct http_dl_ctx_s * con = dl - > ctx ;
char buffer [ 256 ] ;
char Location [ 256 ] ;
2013-11-28 00:45:22 +00:00
char mimetype [ 256 ] ;
2010-03-14 14:35:56 +00:00
char * nl ;
char * msg ;
int ammount ;
2016-07-12 00:40:13 +00:00
# ifdef MULTITHREAD
//if we're running in a thread, wait for some actual activity instead of busylooping like an moron.
if ( dl - > threadctx )
{
struct timeval timeout ;
fd_set rdset , wrset ;
FD_ZERO ( & wrset ) ;
FD_ZERO ( & rdset ) ;
FD_SET ( con - > sock , & wrset ) ; // network socket
FD_SET ( con - > sock , & rdset ) ; // network socket
timeout . tv_sec = 0 ;
timeout . tv_usec = 0.1 * 1000 * 1000 ;
if ( con - > state = = HC_REQUESTING )
select ( con - > sock + 1 , & rdset , & wrset , NULL , & timeout ) ; //wake up when we can read OR write
else
select ( con - > sock + 1 , & rdset , NULL , NULL , & timeout ) ; //wake when we can read.
//note that https should wake up more often, but we don't want to wake up because we *can* write when we're reading without any need to write.
}
# endif
2010-03-14 14:35:56 +00:00
switch ( con - > state )
{
case HC_REQUESTING :
2015-01-02 05:57:14 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
ammount = VFS_WRITE ( con - > stream , con - > buffer , con - > bufferused ) ;
2014-12-25 20:10:05 +00:00
if ( ! ammount )
return true ;
if ( ammount < 0 )
return false ;
# else
2010-03-14 14:35:56 +00:00
ammount = send ( con - > sock , con - > buffer , con - > bufferused , 0 ) ;
2014-12-25 20:10:05 +00:00
2010-03-14 14:35:56 +00:00
if ( ! ammount )
return false ;
if ( ammount < 0 )
{
2014-02-07 08:38:40 +00:00
if ( neterrno ( ) ! = NET_EWOULDBLOCK )
2010-03-14 14:35:56 +00:00
return false ;
return true ;
}
2014-12-25 20:10:05 +00:00
# endif
2010-03-14 14:35:56 +00:00
con - > bufferused - = ammount ;
memmove ( con - > buffer , con - > buffer + ammount , con - > bufferused ) ;
if ( ! con - > bufferused ) //that's it, all sent.
con - > state = HC_GETTINGHEADER ;
break ;
case HC_GETTINGHEADER :
if ( con - > bufferlen - con - > bufferused < 1530 )
ExpandBuffer ( con , 1530 ) ;
2015-01-02 05:57:14 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
ammount = VFS_READ ( con - > stream , con - > buffer + con - > bufferused , con - > bufferlen - con - > bufferused - 15 ) ;
2014-12-25 20:10:05 +00:00
if ( ! ammount )
return true ;
if ( ammount < 0 )
return false ;
# else
2010-03-14 14:35:56 +00:00
ammount = recv ( con - > sock , con - > buffer + con - > bufferused , con - > bufferlen - con - > bufferused - 15 , 0 ) ;
if ( ! ammount )
return false ;
if ( ammount < 0 )
{
2014-02-07 08:38:40 +00:00
if ( neterrno ( ) ! = NET_EWOULDBLOCK )
2010-03-14 14:35:56 +00:00
return false ;
return true ;
}
2014-12-25 20:10:05 +00:00
# endif
2010-03-14 14:35:56 +00:00
con - > bufferused + = ammount ;
con - > buffer [ con - > bufferused ] = ' \0 ' ;
//have we got the entire thing yet?
msg = con - > buffer ;
con - > chunking = false ;
2016-07-12 00:40:13 +00:00
con - > contentlength = - 1 ; //means unspecified
2013-11-28 00:45:22 +00:00
con - > gzip = false ;
* mimetype = 0 ;
* Location = 0 ;
2010-03-14 14:35:56 +00:00
if ( strnicmp ( msg , " HTTP/ " , 5 ) )
2015-08-24 21:44:22 +00:00
{ //pre version 1 (lame servers). no response headers at all.
2010-03-14 14:35:56 +00:00
con - > state = HC_GETTING ;
2010-05-01 22:47:47 +00:00
dl - > status = DL_ACTIVE ;
2013-06-23 02:17:02 +00:00
dl - > replycode = 200 ;
2010-03-14 14:35:56 +00:00
}
else
{
2015-08-24 21:44:22 +00:00
//check if the headers are complete or not
2013-06-23 02:17:02 +00:00
qboolean hcomplete = false ;
2010-03-14 14:35:56 +00:00
while ( * msg )
{
if ( * msg = = ' \n ' )
{
if ( msg [ 1 ] = = ' \n ' )
{ //tut tut, not '\r'? that's not really allowed...
2013-06-23 02:17:02 +00:00
msg + = 2 ;
hcomplete = true ;
2010-03-14 14:35:56 +00:00
break ;
}
2013-06-23 02:17:02 +00:00
if ( msg [ 1 ] = = ' \r ' & & msg [ 2 ] = = ' \n ' )
2010-03-14 14:35:56 +00:00
{
2013-06-23 02:17:02 +00:00
msg + = 3 ;
hcomplete = true ;
2010-03-14 14:35:56 +00:00
break ;
}
2013-06-23 02:17:02 +00:00
msg + + ;
2010-03-14 14:35:56 +00:00
}
2013-06-23 02:17:02 +00:00
while ( * msg = = ' ' | | * msg = = ' \t ' )
msg + + ;
nl = strchr ( msg , ' \n ' ) ;
if ( ! nl )
break ; //not complete, don't bother trying to parse it.
2015-08-24 21:44:22 +00:00
msg = nl ;
}
if ( ! hcomplete )
break ; //headers not complete. break out of switch
//okay, they're complete, woot. we need to actually go through the headers now
msg = con - > buffer ;
while ( * msg )
{
if ( * msg = = ' \n ' )
{
if ( msg [ 1 ] = = ' \n ' )
{ //tut tut, not '\r'? that's not really allowed...
msg + = 2 ;
break ;
}
if ( msg [ 1 ] = = ' \r ' & & msg [ 2 ] = = ' \n ' )
{
msg + = 3 ;
break ;
}
msg + + ;
}
while ( * msg = = ' ' | | * msg = = ' \t ' )
msg + + ;
nl = strchr ( msg , ' \n ' ) ;
if ( ! nl )
break ; //not complete, don't bother trying to parse it.
2010-03-14 14:35:56 +00:00
if ( ! strnicmp ( msg , " Content-Length: " , 16 ) )
con - > contentlength = atoi ( msg + 16 ) ;
2013-11-28 00:45:22 +00:00
else if ( ! strnicmp ( msg , " Content-Type: " , 13 ) )
{
* nl = ' \0 ' ;
2014-10-05 20:04:11 +00:00
COM_TrimString ( msg + 13 , mimetype , sizeof ( mimetype ) ) ;
2013-11-28 00:45:22 +00:00
* nl = ' \n ' ;
}
2010-03-14 14:35:56 +00:00
else if ( ! strnicmp ( msg , " Location: " , 10 ) )
{
2013-06-23 02:17:02 +00:00
* nl = ' \0 ' ;
2014-10-05 20:04:11 +00:00
COM_TrimString ( msg + 10 , Location , sizeof ( Location ) ) ;
2013-06-23 02:17:02 +00:00
* nl = ' \n ' ;
}
else if ( ! strnicmp ( msg , " Content-Encoding: " , 18 ) )
{
char * chunk = strstr ( msg , " gzip " ) ;
if ( chunk < nl )
con - > gzip = true ;
2010-03-14 14:35:56 +00:00
}
else if ( ! strnicmp ( msg , " Transfer-Encoding: " , 19 ) )
{
char * chunk = strstr ( msg , " chunked " ) ;
2013-06-23 02:17:02 +00:00
if ( chunk < nl )
con - > chunking = true ;
2010-03-14 14:35:56 +00:00
}
2015-08-25 03:02:19 +00:00
# ifdef COOKIECOOKIECOOKIE
2015-08-24 21:44:22 +00:00
else if ( ! strnicmp ( msg , " Set-Cookie: " , 12 ) )
{
Cookie_Parse ( con - > server , con - > secure , msg + 12 , nl ) ;
}
2015-08-25 03:02:19 +00:00
# endif
2013-06-23 02:17:02 +00:00
msg = nl ;
2010-03-14 14:35:56 +00:00
}
2013-06-23 02:17:02 +00:00
if ( ! hcomplete )
break ; //headers not complete. break out of switch
2010-03-14 14:35:56 +00:00
ammount = msg - con - > buffer ;
msg = COM_ParseOut ( con - > buffer , buffer , sizeof ( buffer ) ) ;
msg = COM_ParseOut ( msg , buffer , sizeof ( buffer ) ) ;
2012-11-27 03:23:19 +00:00
dl - > replycode = atoi ( buffer ) ;
2010-03-14 14:35:56 +00:00
if ( ! stricmp ( buffer , " 100 " ) )
{ //http/1.1 servers can give this. We ignore it.
con - > bufferused - = ammount ;
memmove ( con - > buffer , con - > buffer + ammount , con - > bufferused ) ;
return true ;
}
if ( ! stricmp ( buffer , " 301 " ) | | ! stricmp ( buffer , " 302 " ) | | ! stricmp ( buffer , " 303 " ) )
{
2014-10-05 20:04:11 +00:00
char trimmed [ 256 ] ;
2010-03-14 14:35:56 +00:00
nl = strchr ( msg , ' \n ' ) ;
if ( nl )
* nl = ' \0 ' ;
2015-06-04 06:15:14 +00:00
Con_Printf ( " HTTP: %s %s (%s) \n " , buffer , COM_TrimString ( msg , trimmed , sizeof ( trimmed ) ) , Location ) ;
2010-03-14 14:35:56 +00:00
if ( ! * Location )
Con_Printf ( " Server redirected to null location \n " ) ;
else
{
HTTP_Cleanup ( dl ) ;
2010-05-01 22:47:47 +00:00
if ( * Location = = ' / ' )
{
char * cur = * dl - > redir ? dl - > redir : dl - > url ;
char * curserver = cur ;
char * curpath ;
/*same server+protocol*/
if ( ! strncmp ( curserver , " http:// " , 7 ) )
curserver + = 7 ;
curpath = strchr ( curserver , ' / ' ) ;
if ( ! curpath )
curpath = curserver + strlen ( curserver ) ;
if ( cur = = dl - > redir )
* curpath = 0 ;
else
Q_strncpyz ( dl - > redir , cur , ( curpath - cur ) + 1 ) ;
Q_strncatz ( dl - > redir , Location , sizeof ( dl - > redir ) ) ;
}
else
Q_strncpyz ( dl - > redir , Location , sizeof ( dl - > redir ) ) ;
2013-06-23 02:17:02 +00:00
dl - > poll = DL_Decide ;
2010-05-01 22:47:47 +00:00
dl - > status = DL_PENDING ;
2010-03-14 14:35:56 +00:00
}
return true ;
}
if ( stricmp ( buffer , " 200 " ) )
{
nl = strchr ( msg , ' \n ' ) ;
if ( ! nl )
return false ; //eh?
if ( nl > msg & & nl [ - 1 ] = = ' \r ' )
nl - - ;
* nl = ' \0 ' ;
Con_Printf ( " HTTP: %s%s \n " , buffer , msg ) ;
return false ; //something went wrong.
}
2016-07-12 00:40:13 +00:00
if ( con - > contentlength ! = - 1 & & con - > contentlength > dl - > sizelimit )
{
dl - > replycode = 413 ; //413 Payload Too Large
Con_Printf ( " HTTP: request exceeds size limit \n " ) ;
return false ; //something went wrong.
}
2010-03-14 14:35:56 +00:00
con - > bufferused - = ammount ;
2010-05-01 22:47:47 +00:00
dl - > totalsize = con - > contentlength ;
2013-06-23 02:17:02 +00:00
memmove ( con - > buffer , con - > buffer + ammount , con - > bufferused ) ;
}
2013-11-28 00:45:22 +00:00
if ( dl - > notifystarted )
2015-05-14 03:06:58 +00:00
{
if ( ! dl - > notifystarted ( dl , * mimetype ? mimetype : NULL ) )
{
dl - > notifycomplete = NULL ;
dl - > status = DL_FAILED ;
return false ;
}
}
2013-11-28 00:45:22 +00:00
2013-06-23 02:17:02 +00:00
if ( ! dl - > file )
{
2012-11-27 03:23:19 +00:00
# ifndef NPFTE
2013-06-23 02:17:02 +00:00
if ( * dl - > localname )
{
FS_CreatePath ( dl - > localname , FS_GAME ) ;
dl - > file = FS_OpenVFS ( dl - > localname , " w+b " , FS_GAME ) ;
}
else
dl - > file = FS_OpenTemp ( ) ;
2012-11-27 03:23:19 +00:00
# endif
2013-06-23 02:17:02 +00:00
if ( ! dl - > file )
{
2013-12-03 15:15:50 +00:00
if ( * dl - > localname )
Con_Printf ( " HTTP: Couldn't open file \" %s \" \n " , dl - > localname ) ;
else
Con_Printf ( " HTTP: Couldn't open temporary file \n " ) ;
2013-06-23 02:17:02 +00:00
dl - > status = DL_FAILED ;
return false ;
2010-03-14 14:35:56 +00:00
}
2013-06-23 02:17:02 +00:00
}
2010-03-14 14:35:56 +00:00
2013-06-23 02:17:02 +00:00
if ( con - > gzip )
{
2017-02-19 00:15:42 +00:00
# ifdef AVAIL_GZDEC
dpp7: Treat 'dropped' c2s packets as choked when using dpp7 protocols. This is because the protocol provides no way to disambiguate, and I don't like false reports of packetloss (only reliables loss can be detected, and that's not frequent enough to be meaningful). Pings can still be determined with dpp7, for those few packets which are acked.
package manager: reworked to enable/disable plugins when downloaded, which can also be present-but-disabled.
package manager: display a confirmation prompt before applying changes. do not allow other changes to be made while applying. prompt may be skipped with 'pkg apply' in dedicated servers.
sv: downloads are no longer forced to lower case.
sv: added sv_demoAutoCompress cvar. set to 1 to directly record to *.mvd.gz
cl: properly support directly playing .mvd.gz files
menus: reworked to separate mouse and keyboard focus. mouse focus becomes keyboard focus only on mouse clicks. tooltips follow mouse cursors.
menus: cleaned up menu heirachy a little. now simpler.
server browser: changed 'hide *' filters to 'show *' instead. I felt it was more logical.
deluxmapping: changed to disabled, load, generate, like r_loadlit is.
render targets api now supports negative formats to mean nearest filtering, where filtering is part of texture state.
drawrotpic fixed, now batches and interacts with drawpic correctly.
drawline fixed, no interacts with draw* correctly, but still does not batch.
fixed saving games.
provide proper userinfo to nq clients, where supported.
qcc: catch string table overflows safely, giving errors instead of crashes. switch to 32bit statements if some over-sized function requires it.
qtv: some bigcoords support tweaks
git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5073 fc73d0e0-1445-4013-8a0c-d673dee63da5
2017-03-21 05:27:07 +00:00
con - > file = FS_GZ_WriteFilter ( dl - > file , false , false ) ;
2013-11-21 23:02:28 +00:00
# else
Con_Printf ( " HTTP: no support for gzipped files \" %s \" \n " , dl - > localname ) ;
2013-11-28 00:45:22 +00:00
dl - > status = DL_FAILED ;
return false ;
2013-06-23 02:17:02 +00:00
# endif
2010-03-14 14:35:56 +00:00
}
2013-06-23 02:17:02 +00:00
else
con - > file = dl - > file ;
con - > state = HC_GETTING ;
dl - > status = DL_ACTIVE ;
2010-03-14 14:35:56 +00:00
//Fall through
case HC_GETTING :
if ( con - > bufferlen - con - > bufferused < 1530 )
ExpandBuffer ( con , 1530 ) ;
2015-01-02 05:57:14 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
ammount = VFS_READ ( con - > stream , con - > buffer + con - > bufferused , con - > bufferlen - con - > bufferused - 1 ) ;
2014-12-25 20:10:05 +00:00
if ( ammount = = 0 )
return true ; //no data yet
else if ( ammount < 0 )
ammount = 0 ; //error (EOF?)
# else
2010-03-14 14:35:56 +00:00
ammount = recv ( con - > sock , con - > buffer + con - > bufferused , con - > bufferlen - con - > bufferused - 1 , 0 ) ;
if ( ammount < 0 )
{
2014-02-07 08:38:40 +00:00
if ( neterrno ( ) ! = NET_EWOULDBLOCK )
2010-03-14 14:35:56 +00:00
return false ;
return true ;
}
2014-12-25 20:10:05 +00:00
# endif
2010-03-14 14:35:56 +00:00
con - > bufferused + = ammount ;
if ( con - > chunking ) //FIXME: NEEDS TESTING!!!
{
int trim ;
char * nl ;
con - > buffer [ con - > bufferused ] = ' \0 ' ;
for ( ; ; )
{ //work out as we go.
if ( con - > chunksize ) //we are trying to parse a chunk.
{
trim = con - > bufferused - con - > chunked ;
if ( trim > con - > chunksize )
trim = con - > chunksize ; //don't go into the next size field.
con - > chunksize - = trim ;
con - > chunked + = trim ;
if ( ! con - > chunksize )
{ //we need to find the next \n and trim it.
nl = strchr ( con - > buffer + con - > chunked , ' \n ' ) ;
if ( ! nl )
break ;
nl + + ;
trim = nl - ( con - > buffer + con - > chunked ) ;
memmove ( con - > buffer + con - > chunked , nl , con - > buffer + con - > bufferused - nl + 1 ) ;
con - > bufferused - = trim ;
}
if ( ! ( con - > bufferused - con - > chunked ) )
break ;
}
else
{
nl = strchr ( con - > buffer + con - > chunked , ' \n ' ) ;
if ( ! nl )
break ;
con - > chunksize = strtol ( con - > buffer + con - > chunked , NULL , 16 ) ; //it's hex.
nl + + ;
trim = nl - ( con - > buffer + con - > chunked ) ;
memmove ( con - > buffer + con - > chunked , nl , con - > buffer + con - > bufferused - nl + 1 ) ;
con - > bufferused - = trim ;
}
}
con - > totalreceived + = con - > chunked ;
2016-07-12 00:40:13 +00:00
if ( con - > totalreceived > dl - > sizelimit )
{
dl - > replycode = 413 ; //413 Payload Too Large
Con_Printf ( " HTTP: request exceeds size limit \n " ) ;
return false ; //something went wrong.
}
2013-06-23 02:17:02 +00:00
if ( con - > file & & con - > chunked ) //we've got a chunk in the buffer
2010-03-14 14:35:56 +00:00
{ //write it
2013-06-23 02:17:02 +00:00
if ( VFS_WRITE ( con - > file , con - > buffer , con - > chunked ) ! = con - > chunked )
2010-03-14 14:35:56 +00:00
{
Con_Printf ( " Write error whilst downloading %s \n Disk full? \n " , dl - > localname ) ;
return false ;
}
//and move the unparsed chunk to the front.
con - > bufferused - = con - > chunked ;
memmove ( con - > buffer , con - > buffer + con - > chunked , con - > bufferused ) ;
con - > chunked = 0 ;
}
}
else
{
2016-07-12 00:40:13 +00:00
int chunk = con - > bufferused ;
if ( con - > contentlength ! = - 1 & & chunk > con - > contentlength - con - > totalreceived )
chunk = con - > contentlength - con - > totalreceived ;
con - > totalreceived + = chunk ;
if ( con - > totalreceived > dl - > sizelimit )
{
dl - > replycode = 413 ; //413 Payload Too Large
Con_Printf ( " HTTP: request exceeds size limit \n " ) ;
return false ; //something went wrong.
}
2013-06-23 02:17:02 +00:00
if ( con - > file ) //we've got a chunk in the buffer
2010-03-14 14:35:56 +00:00
{ //write it
2016-07-12 00:40:13 +00:00
if ( VFS_WRITE ( con - > file , con - > buffer , chunk ) ! = chunk )
2010-03-14 14:35:56 +00:00
{
Con_Printf ( " Write error whilst downloading %s \n Disk full? \n " , dl - > localname ) ;
return false ;
}
2016-07-12 00:40:13 +00:00
//and move the unparsed chunk to the front.
memmove ( con - > buffer , con - > buffer + con - > bufferused , con - > bufferused - chunk ) ;
con - > bufferused - = chunk ;
2010-03-14 14:35:56 +00:00
}
2015-04-14 23:12:17 +00:00
if ( con - > totalreceived = = con - > contentlength )
ammount = 0 ;
2010-03-14 14:35:56 +00:00
}
if ( ! ammount )
{ //server closed off the connection.
if ( con - > chunksize )
dl - > status = DL_FAILED ;
else
2013-06-23 02:17:02 +00:00
{
dpp7: Treat 'dropped' c2s packets as choked when using dpp7 protocols. This is because the protocol provides no way to disambiguate, and I don't like false reports of packetloss (only reliables loss can be detected, and that's not frequent enough to be meaningful). Pings can still be determined with dpp7, for those few packets which are acked.
package manager: reworked to enable/disable plugins when downloaded, which can also be present-but-disabled.
package manager: display a confirmation prompt before applying changes. do not allow other changes to be made while applying. prompt may be skipped with 'pkg apply' in dedicated servers.
sv: downloads are no longer forced to lower case.
sv: added sv_demoAutoCompress cvar. set to 1 to directly record to *.mvd.gz
cl: properly support directly playing .mvd.gz files
menus: reworked to separate mouse and keyboard focus. mouse focus becomes keyboard focus only on mouse clicks. tooltips follow mouse cursors.
menus: cleaned up menu heirachy a little. now simpler.
server browser: changed 'hide *' filters to 'show *' instead. I felt it was more logical.
deluxmapping: changed to disabled, load, generate, like r_loadlit is.
render targets api now supports negative formats to mean nearest filtering, where filtering is part of texture state.
drawrotpic fixed, now batches and interacts with drawpic correctly.
drawline fixed, no interacts with draw* correctly, but still does not batch.
fixed saving games.
provide proper userinfo to nq clients, where supported.
qcc: catch string table overflows safely, giving errors instead of crashes. switch to 32bit statements if some over-sized function requires it.
qtv: some bigcoords support tweaks
git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5073 fc73d0e0-1445-4013-8a0c-d673dee63da5
2017-03-21 05:27:07 +00:00
# ifdef AVAIL_GZDEC
2015-04-14 23:12:17 +00:00
if ( con - > gzip & & con - > file )
{
VFS_CLOSE ( con - > file ) ;
con - > file = NULL ;
}
# endif
if ( con - > contentlength ! = - 1 & & con - > totalreceived ! = con - > contentlength )
dl - > status = DL_FAILED ; //file was truncated
else
dl - > status = ( dl - > replycode = = 200 ) ? DL_FINISHED : DL_FAILED ;
2013-06-23 02:17:02 +00:00
}
2010-03-14 14:35:56 +00:00
return false ;
}
2010-05-01 22:47:47 +00:00
dl - > completed = con - > totalreceived ;
2010-03-14 14:35:56 +00:00
break ;
}
return true ;
}
void HTTPDL_Establish ( struct dl_download * dl )
{
2015-01-02 05:57:14 +00:00
# ifdef NPFTE
unsigned long _true = true ;
struct sockaddr_qstorage serveraddr ;
int addressfamily ;
int addresssize ;
# endif
2010-03-14 14:35:56 +00:00
struct http_dl_ctx_s * con ;
2014-12-25 20:10:05 +00:00
qboolean https = false ;
2010-03-14 14:35:56 +00:00
2015-08-25 03:02:19 +00:00
# ifdef COOKIECOOKIECOOKIE
2015-08-24 21:44:22 +00:00
char cookies [ 8192 ] ;
2015-08-25 03:02:19 +00:00
# else
char * cookies = " " ;
# endif
2010-03-14 14:35:56 +00:00
char uri [ MAX_OSPATH ] ;
char * slash ;
const char * url = dl - > redir ;
if ( ! * url )
url = dl - > url ;
2014-12-25 20:10:05 +00:00
if ( ! strnicmp ( url , " https:// " , 8 ) )
{
url + = 8 ;
https = true ;
}
else if ( ! strnicmp ( url , " http:// " , 7 ) )
2010-03-14 14:35:56 +00:00
url + = 7 ;
2015-08-24 21:44:22 +00:00
con = malloc ( sizeof ( * con ) ) ;
memset ( con , 0 , sizeof ( * con ) ) ;
2010-03-14 14:35:56 +00:00
slash = strchr ( url , ' / ' ) ;
if ( ! slash )
{
2015-08-24 21:44:22 +00:00
Q_strncpyz ( con - > server , url , sizeof ( con - > server ) ) ;
2010-03-14 14:35:56 +00:00
Q_strncpyz ( uri , " / " , sizeof ( uri ) ) ;
}
else
{
Q_strncpyz ( uri , slash , sizeof ( uri ) ) ;
2015-08-24 21:44:22 +00:00
Q_strncpyz ( con - > server , url , sizeof ( con - > server ) ) ;
con - > server [ slash - url ] = ' \0 ' ;
2010-03-14 14:35:56 +00:00
}
dl - > ctx = con ;
2012-11-27 03:23:19 +00:00
dl - > abort = HTTP_Cleanup ;
dl - > status = DL_RESOLVING ;
2010-03-14 14:35:56 +00:00
2015-01-02 05:57:14 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
con - > sock = INVALID_SOCKET ;
con - > stream = NULL ;
con - > secure = false ;
# ifndef HAVE_SSL
if ( ! https )
2015-01-02 05:57:14 +00:00
# endif
2016-07-12 00:40:13 +00:00
{
netadr_t adr = { 0 } ;
//fixme: support more than one address possibility?
//https uses a different default port
if ( NET_StringToAdr2 ( con - > server , https ? 443 : 80 , & adr , 1 ) )
con - > sock = TCP_OpenStream ( & adr ) ;
con - > stream = FS_OpenTCPSocket ( con - > sock , true , con - > server ) ;
2014-12-25 20:10:05 +00:00
}
2016-07-12 00:40:13 +00:00
# ifdef HAVE_SSL
if ( https )
2014-12-25 20:10:05 +00:00
{
2016-07-12 00:40:13 +00:00
//https has an extra ssl/tls layer between tcp and http.
con - > stream = FS_OpenSSL ( con - > server , con - > stream , false , false ) ;
con - > secure = true ;
2014-12-25 20:10:05 +00:00
}
2016-07-12 00:40:13 +00:00
# endif
if ( ! con - > stream )
2015-01-02 05:57:14 +00:00
{
dl - > status = DL_FAILED ;
return ;
}
2014-12-25 20:10:05 +00:00
# else
2015-08-24 21:44:22 +00:00
con - > secure = false ;
if ( https | | ! NET_StringToSockaddr ( con - > server , 80 , & serveraddr , & addressfamily , & addresssize ) )
2010-03-14 14:35:56 +00:00
{
dl - > status = DL_FAILED ;
return ;
}
dl - > status = DL_QUERY ;
2012-11-27 03:23:19 +00:00
if ( ( con - > sock = socket ( addressfamily , SOCK_STREAM , IPPROTO_TCP ) ) = = - 1 )
{
dl - > status = DL_FAILED ;
return ;
}
//don't bother binding. its optional.
2014-05-16 17:45:37 +00:00
//FIXME: make the connect call with a non-blocking socket.
//FIXME: use a vfsfile_t instead of a direct socket to support https
2010-03-14 14:35:56 +00:00
//not yet blocking.
2012-11-27 03:23:19 +00:00
if ( connect ( con - > sock , ( struct sockaddr * ) & serveraddr , addresssize ) = = - 1 )
2010-03-14 14:35:56 +00:00
{
2014-03-30 08:55:06 +00:00
int err = neterrno ( ) ;
switch ( err )
{
case NET_EACCES :
2015-08-24 21:44:22 +00:00
Con_Printf ( " HTTP: connect(%s): access denied. Check firewall. \n " , con - > server ) ;
2014-03-30 08:55:06 +00:00
break ;
2014-05-16 17:45:37 +00:00
case NET_ETIMEDOUT :
2015-08-24 21:44:22 +00:00
Con_Printf ( " HTTP: connect(%s): timed out. \n " , con - > server ) ;
2014-05-16 17:45:37 +00:00
break ;
2014-03-30 08:55:06 +00:00
default :
2015-08-24 21:44:22 +00:00
Con_Printf ( " HTTP: connect(%s): %s " , con - > server , strerror ( neterrno ( ) ) ) ;
2014-03-30 08:55:06 +00:00
break ;
}
2010-03-14 14:35:56 +00:00
dl - > status = DL_FAILED ;
return ;
}
if ( ioctlsocket ( con - > sock , FIONBIO , & _true ) = = - 1 ) //now make it non blocking.
{
dl - > status = DL_FAILED ;
return ;
}
2014-12-25 20:10:05 +00:00
# endif
2015-08-25 03:02:19 +00:00
# ifdef COOKIECOOKIECOOKIE
2015-08-24 21:44:22 +00:00
Cookie_Regurgitate ( con - > server , con - > secure , cookies , sizeof ( cookies ) ) ;
2015-08-25 03:02:19 +00:00
# endif
2013-10-08 14:28:11 +00:00
if ( dl - > postdata )
{
2015-08-24 21:44:22 +00:00
ExpandBuffer ( con , 1024 + strlen ( uri ) + strlen ( con - > server ) + strlen ( cookies ) + strlen ( dl - > postmimetype ) + dl - > postlen ) ;
2013-10-08 14:28:11 +00:00
Q_snprintfz ( con - > buffer , con - > bufferlen ,
" POST %s HTTP/1.1 \r \n "
" Host: %s \r \n "
2015-08-24 21:44:22 +00:00
/*Cookie:*/ " %s "
2014-03-30 00:39:37 +00:00
" Content-Length: %u \r \n "
2013-10-08 14:28:11 +00:00
" Content-Type: %s \r \n "
" Connection: close \r \n "
2017-02-19 00:15:42 +00:00
# ifdef AVAIL_GZDEC
2013-10-08 14:28:11 +00:00
" Accept-Encoding: gzip \r \n "
# endif
" User-Agent: " FULLENGINENAME " \r \n "
2015-08-24 21:44:22 +00:00
" \r \n " , uri , con - > server , cookies , ( unsigned int ) dl - > postlen , dl - > postmimetype ) ;
2013-10-08 14:28:11 +00:00
con - > bufferused = strlen ( con - > buffer ) ;
memcpy ( con - > buffer + con - > bufferused , dl - > postdata , dl - > postlen ) ;
con - > bufferused + = dl - > postlen ;
}
else
{
ExpandBuffer ( con , 512 * 1024 ) ;
Q_snprintfz ( con - > buffer , con - > bufferlen ,
" GET %s HTTP/1.1 \r \n "
" Host: %s \r \n "
2015-08-24 21:44:22 +00:00
/*Cookie:*/ " %s "
2015-05-14 03:06:58 +00:00
" Connection: close \r \n " //theoretically, this is not needed. but as our code will basically do it anyway, it might as well be here FIXME: implement connection reuse.
2017-02-19 00:15:42 +00:00
# ifdef AVAIL_GZDEC
2013-10-08 14:28:11 +00:00
" Accept-Encoding: gzip \r \n "
# endif
" User-Agent: " FULLENGINENAME " \r \n "
2015-08-24 21:44:22 +00:00
" \r \n " , uri , con - > server , cookies ) ;
2013-10-08 14:28:11 +00:00
con - > bufferused = strlen ( con - > buffer ) ;
}
2010-03-14 14:35:56 +00:00
con - > contentlength = - 1 ;
}
qboolean HTTPDL_Poll ( struct dl_download * dl )
{
/*failed previously*/
if ( dl - > status = = DL_FAILED )
return false ;
if ( ! dl - > ctx )
{
HTTPDL_Establish ( dl ) ;
if ( dl - > status = = DL_FAILED )
{
HTTP_Cleanup ( dl ) ;
2010-03-25 22:56:11 +00:00
dl - > status = DL_FAILED ;
2010-03-14 14:35:56 +00:00
return false ;
}
}
if ( dl - > ctx )
{
2010-05-01 22:47:47 +00:00
if ( ! HTTP_DL_Work ( dl ) )
if ( dl - > status ! = DL_FINISHED )
dl - > status = DL_FAILED ;
2010-03-14 14:35:56 +00:00
if ( dl - > status = = DL_FAILED )
{
HTTP_Cleanup ( dl ) ;
2010-03-25 22:56:11 +00:00
dl - > status = DL_FAILED ;
2010-03-14 14:35:56 +00:00
return false ;
}
if ( dl - > status = = DL_FINISHED )
return false ;
}
return true ;
}
2014-09-08 23:47:19 +00:00
/*
//decode a base64 byte to a 0-63 value. Cannot cope with =.
static unsigned int Base64_DecodeByte ( char byt )
{
if ( byt > = ' A ' & & byt < = ' Z ' )
return ( byt - ' A ' ) + 0 ;
if ( byt > = ' a ' & & byt < = ' z ' )
return ( byt - ' a ' ) + 26 ;
if ( byt > = ' 0 ' & & byt < = ' 9 ' )
return ( byt - ' 0 ' ) + 52 ;
if ( byt = = ' + ' )
return 62 ;
if ( byt = = ' / ' )
return 63 ;
return - 1 ;
}
//FIXME: we should be able to skip whitespace.
static int Base64_Decode ( char * out , int outlen , char * * srcout , int * srclenout )
{
int len = 0 ;
unsigned int result ;
char * src = * srcout ;
int srclen = * srclenout ;
//4 input chars give 3 output chars
while ( srclen > = 4 )
{
if ( len + 3 > outlen )
{
//ran out of space in the output buffer
* srcout = src ;
* srclenout = srclen ;
return len ;
}
result = Base64_DecodeByte ( src [ 0 ] ) < < 18 ;
result | = Base64_DecodeByte ( src [ 1 ] ) < < 12 ;
out [ len + + ] = ( result > > 16 ) & 0xff ;
if ( src [ 2 ] ! = ' = ' )
{
result | = Base64_DecodeByte ( src [ 2 ] ) < < 6 ;
out [ len + + ] = ( result > > 8 ) & 0xff ;
if ( src [ 3 ] ! = ' = ' )
{
result | = Base64_DecodeByte ( src [ 3 ] ) < < 0 ;
out [ len + + ] = ( result > > 0 ) & 0xff ;
}
}
if ( result & 0xff000000 )
return 0 ; //some kind of invalid char
src + = 4 ;
srclen - = 4 ;
}
//end of string
* srcout = src ;
* srclenout = srclen ;
//some kind of error
if ( srclen )
{
if ( srclen ! = 1 | | * src )
return 0 ;
}
return len ;
}
qboolean DataScheme_Decode ( struct dl_download * dl )
{
char block [ 8192 ] ;
int remaining , blocksize ;
char mimetype [ 256 ] ;
char baseval [ 256 ] ;
char charsetval [ 256 ] ;
char * url ;
char * data ;
char * charset ;
char * base ;
//failed previously
if ( dl - > status = = DL_FAILED )
return false ;
//data:[<MIME-type>][;charset=<encoding>][;base64],<data>
* mimetype = 0 ;
* baseval = 0 ;
* charsetval = 0 ;
url = dl - > url ;
if ( ! strncmp ( url , " data: " , 5 ) )
url + = 5 ; //should always match
data = strchr ( url , ' , ' ) ;
if ( ! data )
return false ;
charset = memchr ( url , ' ; ' , data - url ) ;
if ( charset )
{
base = memchr ( charset + 1 , ' ; ' , data - charset ) ;
if ( base )
{
if ( data - ( base + 1 ) > = sizeof ( baseval ) )
return false ;
memcpy ( baseval , base + 1 , data - ( base + 1 ) ) ;
baseval [ data - ( base + 1 ) ] = 0 ;
}
else
base = data ;
if ( base - ( charset + 1 ) > = sizeof ( charsetval ) )
return false ;
memcpy ( charsetval , charset + 1 , base - ( charset + 1 ) ) ;
charsetval [ base - ( charset + 1 ) ] = 0 ;
if ( ! strchr ( charsetval , ' = ' ) )
{
strcpy ( baseval , charsetval ) ;
* charsetval = 0 ;
}
}
else
charset = data ;
if ( charset - ( url ) > = sizeof ( charsetval ) )
return false ;
memcpy ( mimetype , url , charset - ( url ) ) ;
mimetype [ charset - ( url ) ] = 0 ;
if ( ! * mimetype )
Q_strncpyz ( mimetype , " text/plain " , sizeof ( mimetype ) ) ;
if ( ! * charsetval )
Q_strncpyz ( charsetval , " charset=US-ASCII " , sizeof ( charsetval ) ) ;
if ( dl - > notifystarted )
dl - > notifystarted ( dl , * mimetype ? mimetype : NULL ) ;
if ( ! dl - > file )
{
# ifndef NPFTE
if ( * dl - > localname )
{
FS_CreatePath ( dl - > localname , FS_GAME ) ;
dl - > file = FS_OpenVFS ( dl - > localname , " w+b " , FS_GAME ) ;
}
else
dl - > file = FS_OpenTemp ( ) ;
# endif
if ( ! dl - > file )
{
if ( * dl - > localname )
Con_Printf ( " HTTP: Couldn't open file \" %s \" \n " , dl - > localname ) ;
else
Con_Printf ( " HTTP: Couldn't open temporary file \n " ) ;
dl - > status = DL_FAILED ;
return false ;
}
}
data + + ;
remaining = strlen ( data ) ;
while ( remaining > 0 )
{
blocksize = Base64_Decode ( block , sizeof ( block ) , & data , & remaining ) ;
if ( ! blocksize )
{
dl - > status = DL_FAILED ;
return false ;
}
VFS_WRITE ( dl - > file , block , blocksize ) ;
}
dl - > status = DL_FINISHED ;
return false ;
}
*/
2013-06-23 02:17:02 +00:00
qboolean DL_Decide ( struct dl_download * dl )
2010-03-14 14:35:56 +00:00
{
const char * url = dl - > redir ;
if ( ! * url )
url = dl - > url ;
2014-09-08 23:47:19 +00:00
/*if (!strnicmp(url, "data:", 5))
dl - > poll = DataScheme_Decode ;
else */
2010-03-14 14:35:56 +00:00
if ( ! strnicmp ( url , " http:// " , 7 ) )
dl - > poll = HTTPDL_Poll ;
2014-12-25 20:10:05 +00:00
else if ( ! strnicmp ( url , " https:// " , 7 ) )
dl - > poll = HTTPDL_Poll ;
2010-03-14 14:35:56 +00:00
else
{
dl - > status = DL_FAILED ;
return false ;
}
return true ;
}
2013-03-12 23:09:25 +00:00
# endif /*!defined(NACL)*/
2010-03-14 14:35:56 +00:00
# ifdef MULTITHREAD
2016-07-12 00:40:13 +00:00
static unsigned int dlthreads = 0 ;
# define MAXDOWNLOADTHREADS 4
2015-04-21 21:49:08 +00:00
# if defined(LOADERTHREAD) && !defined(NPFTE)
2015-04-21 04:12:00 +00:00
static void HTTP_Wake_Think ( void * ctx , void * data , size_t a , size_t b )
{
2016-07-12 00:40:13 +00:00
dlthreads - - ;
2015-04-21 04:12:00 +00:00
HTTP_CL_Think ( ) ;
}
2015-04-21 21:49:08 +00:00
# endif
2010-03-14 14:35:56 +00:00
static int DL_Thread_Work ( void * arg )
{
struct dl_download * dl = arg ;
2016-07-12 00:40:13 +00:00
while ( dl - > threadenable )
2010-03-14 14:35:56 +00:00
{
if ( ! dl - > poll ( dl ) )
{
2012-11-27 03:23:19 +00:00
# ifdef NPFTE
//the plugin doesn't have a download loop
2013-11-28 00:45:22 +00:00
if ( dl - > notifycomplete )
2017-01-13 00:39:50 +00:00
{
2013-11-28 00:45:22 +00:00
dl - > notifycomplete ( dl ) ;
2017-01-13 00:39:50 +00:00
dl - > notifycomplete = NULL ;
}
2010-03-14 14:35:56 +00:00
if ( dl - > file )
VFS_CLOSE ( dl - > file ) ;
2012-11-27 03:23:19 +00:00
# else
if ( dl - > status ! = DL_FAILED & & dl - > status ! = DL_FINISHED )
dl - > status = DL_FAILED ;
# endif
2010-03-14 14:35:56 +00:00
break ;
}
}
2016-07-12 00:40:13 +00:00
dl - > threadenable = false ;
2015-04-21 04:12:00 +00:00
2015-04-21 21:49:08 +00:00
# if defined(LOADERTHREAD) && !defined(NPFTE)
2016-02-15 06:01:17 +00:00
COM_AddWork ( WG_MAIN , HTTP_Wake_Think , NULL , NULL , 0 , 0 ) ;
2015-04-21 21:49:08 +00:00
# endif
2010-03-14 14:35:56 +00:00
return 0 ;
}
/*create a thread to perform the given download
to use : call DL_Create ( not HTTP_CL_Get ! ) to get a context , then call this .
note that you need to call DL_Close from another thread , NOT IN THE NOTIFY FUNC .
the file handle must be safe to write to in threads .
*/
qboolean DL_CreateThread ( struct dl_download * dl , vfsfile_t * file , void ( * NotifyFunction ) ( struct dl_download * dl ) )
{
if ( ! dl )
return false ;
------------------------------------------------------------------------
r4169 | acceptthis | 2013-01-17 08:55:12 +0000 (Thu, 17 Jan 2013) | 31 lines
removed MAX_VISEDICTS limit.
PEXT2_REPLACEMENTDELTAS tweaked, now has 4 million entity limit. still not enabled by default.
TE_BEAM now maps to a separate TEQW_BEAM to avoid conflicts with QW.
added android multitouch emulation for windows/rawinput (in_simulatemultitouch).
split topcolor/bottomcolor from scoreboard, for dp's colormap|1024 feature.
now using utf-8 for windows consoles.
qcc warnings/errors now give clickable console links for quick+easy editing.
disabled menutint when the currently active item changes contrast or gamma (for OneManClan).
Added support for drawfont/drawfontscale.
tweaked the qcvm a little to reduce the number of pointers.
.doll file loading. still experimental and will likely crash. requires csqc active, even if its a dummy progs. this will be fixed in time. Still other things that need cleaning up.
windows: gl_font "?" shows the standard windows font-selection dialog, and can be used to select windows fonts. not all work. and you probably don't want to use windings.
fixed splitscreen support when playing mvds. added mini-scoreboards to splitscreen.
editor/debugger now shows asm if there's no linenumber info. also, pressing f1 for help shows the shortcuts.
Added support for .framegroups files for psk(psa) and iqm formats.
True support for ezquake's colour codes. Mutually exclusive with background colours.
path command output slightly more readable.
added support for digest_hex (MD4, SHA1, CRC16).
skingroups now colourmap correctly.
Fix terrain colour hints, and litdata from the wrong bsp.
fix ftp dual-homed issue. support epsv command, and enable ipv6 (eprt still not supported).
remove d3d11 compilation from the makefile. the required headers are not provided by mingw, and are not available to the build bot, so don't bother.
fix v *= v.x and similar opcodes.
fteqcc: fixed support for áéÃóú type chars in names. utf-8 files now properly supported (even with the utf-8 bom/identifier). utf-16 also supported.
fteqcc: fixed '#if 1 == 3 && 4' parsing.
fteqcc: -Werror acts on the warning, rather than as a separate error. Line numbers are thus more readable.
fteqcc: copyright message now includes compile date instead.
fteqccgui: the treeview control is now coloured depending on whether there were warnings/errors in the last compile.
fteqccgui: the output window is now focused and scrolls down as compilation progresses.
pr_dumpplatform command dumps out some pragmas to convert more serious warnings to errors. This is to avoid the infamous 'fteqcc sucks cos my code sucks' issue.
rewrote prespawn/modelist/soundlist code. server tracks progress now.
------------------------------------------------------------------------
git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4167 fc73d0e0-1445-4013-8a0c-d673dee63da5
2013-03-12 22:29:40 +00:00
if ( file )
dl - > file = file ;
if ( NotifyFunction )
2013-11-28 00:45:22 +00:00
dl - > notifycomplete = NotifyFunction ;
2010-03-14 14:35:56 +00:00
2016-07-12 00:40:13 +00:00
dl - > threadenable = true ;
# if defined(LOADERTHREAD) && !defined(NPFTE)
if ( dlthreads < 0 )
# endif
{
dl - > threadctx = Sys_CreateThread ( " download " , DL_Thread_Work , dl , THREADP_NORMAL , 0 ) ;
if ( ! dl - > threadctx )
return false ;
dlthreads + + ;
}
2010-03-14 14:35:56 +00:00
return true ;
}
2016-08-25 00:12:14 +00:00
# else
qboolean DL_CreateThread ( struct dl_download * dl , vfsfile_t * file , void ( * NotifyFunction ) ( struct dl_download * dl ) )
{
if ( ! dl )
return false ;
if ( file )
dl - > file = file ;
if ( NotifyFunction )
dl - > notifycomplete = NotifyFunction ;
return false ;
}
2010-03-14 14:35:56 +00:00
# endif
/*create a standalone download context*/
struct dl_download * DL_Create ( const char * url )
{
struct dl_download * newdl ;
2014-09-08 23:47:19 +00:00
newdl = malloc ( sizeof ( * newdl ) + strlen ( url ) + 1 ) ;
2010-03-14 14:35:56 +00:00
if ( ! newdl )
return NULL ;
memset ( newdl , 0 , sizeof ( * newdl ) ) ;
2014-09-08 23:47:19 +00:00
newdl - > url = ( char * ) ( newdl + 1 ) ;
strcpy ( newdl - > url , url ) ;
2013-06-23 02:17:02 +00:00
newdl - > poll = DL_Decide ;
2016-07-12 00:40:13 +00:00
newdl - > sizelimit = 0x80000000u ; //some sanity limit.
2017-01-16 08:20:35 +00:00
# if !defined(NPFTE) && !defined(SERVERONLY)
2017-01-13 00:39:50 +00:00
newdl - > qdownload . method = DL_HTTP ;
2017-01-16 08:20:35 +00:00
# endif
2010-03-14 14:35:56 +00:00
2013-05-03 04:28:08 +00:00
if ( ! newdl - > poll ( newdl ) )
{
free ( newdl ) ;
newdl = NULL ;
}
2010-03-14 14:35:56 +00:00
return newdl ;
}
2013-03-12 23:09:25 +00:00
2010-03-14 14:35:56 +00:00
/*destroys an entire download context*/
void DL_Close ( struct dl_download * dl )
{
2016-07-12 00:40:13 +00:00
struct dl_download * * link = NULL ;
# ifndef NPFTE
for ( link = & activedownloads ; * link ; link = & ( * link ) - > next )
{
if ( * link = = dl )
{
* link = dl - > next ;
break ;
}
}
# endif
2015-05-16 15:51:39 +00:00
# if !defined(NPFTE) && !defined(SERVERONLY)
2014-06-12 23:08:42 +00:00
if ( cls . download = = & dl - > qdownload )
cls . download = NULL ;
2013-03-12 23:09:25 +00:00
# endif
2010-03-14 14:35:56 +00:00
# ifdef MULTITHREAD
2016-07-12 00:40:13 +00:00
dl - > threadenable = false ;
2010-03-14 14:35:56 +00:00
if ( dl - > threadctx )
Sys_WaitOnThread ( dl - > threadctx ) ;
# endif
2017-01-13 00:39:50 +00:00
if ( dl - > file & & dl - > file - > Seek )
VFS_SEEK ( dl - > file , 0 ) ;
if ( dl - > notifycomplete )
dl - > notifycomplete ( dl ) ;
2010-03-14 14:35:56 +00:00
if ( dl - > abort )
dl - > abort ( dl ) ;
if ( dl - > file )
VFS_CLOSE ( dl - > file ) ;
2013-10-08 14:28:11 +00:00
if ( dl - > postdata )
BZ_Free ( dl - > postdata ) ;
2010-03-14 14:35:56 +00:00
free ( dl ) ;
}
2016-07-17 18:41:01 +00:00
# ifndef NPFTE
2016-07-12 00:40:13 +00:00
void DL_DeThread ( void )
{
# ifdef MULTITHREAD
//if we're about to fork, ensure that any downloads are properly parked so that there's no workers in an unknown state.
struct dl_download * dl ;
for ( dl = activedownloads ; dl ; dl = dl - > next )
{
dl - > threadenable = false ;
if ( dl - > threadctx )
Sys_WaitOnThread ( dl - > threadctx ) ;
}
# endif
}
2010-03-14 14:35:56 +00:00
2012-11-27 03:23:19 +00:00
/*updates pending downloads*/
2016-07-12 00:40:13 +00:00
unsigned int HTTP_CL_GetActiveDownloads ( void )
{
struct dl_download * dl ;
unsigned int count = 0 ;
for ( dl = activedownloads ; dl ; dl = dl - > next )
count + + ;
return count ;
}
2012-11-27 03:23:19 +00:00
2013-11-28 00:45:22 +00:00
/*create a download context and add it to the list, for lazy people. not threaded*/
2010-03-14 14:35:56 +00:00
struct dl_download * HTTP_CL_Get ( const char * url , const char * localfile , void ( * NotifyFunction ) ( struct dl_download * dl ) )
{
struct dl_download * newdl = DL_Create ( url ) ;
if ( ! newdl )
return newdl ;
2013-11-28 00:45:22 +00:00
newdl - > notifycomplete = NotifyFunction ;
2010-03-25 22:56:11 +00:00
if ( localfile )
Q_strncpyz ( newdl - > localname , localfile , sizeof ( newdl - > localname ) ) ;
2010-03-14 14:35:56 +00:00
newdl - > next = activedownloads ;
activedownloads = newdl ;
2012-11-27 03:23:19 +00:00
2014-12-29 02:35:10 +00:00
2015-05-16 15:51:39 +00:00
# ifndef SERVERONLY
2015-04-21 04:12:00 +00:00
if ( ! cls . download & & localfile & & ! newdl - > isquery )
2014-12-29 02:35:10 +00:00
{
cls . download = & newdl - > qdownload ;
if ( * newdl - > localname )
Q_strncpyz ( newdl - > qdownload . localname , newdl - > localname , sizeof ( newdl - > qdownload . localname ) ) ;
else
Q_strncpyz ( newdl - > qdownload . localname , newdl - > url , sizeof ( newdl - > qdownload . localname ) ) ;
Q_strncpyz ( newdl - > qdownload . remotename , newdl - > url , sizeof ( newdl - > qdownload . remotename ) ) ;
newdl - > qdownload . starttime = Sys_DoubleTime ( ) ;
}
2015-05-16 15:51:39 +00:00
# endif
2014-12-29 02:35:10 +00:00
2010-03-14 14:35:56 +00:00
return newdl ;
}
2013-10-08 14:28:11 +00:00
struct dl_download * HTTP_CL_Put ( const char * url , const char * mime , const char * data , size_t datalen , void ( * NotifyFunction ) ( struct dl_download * dl ) )
{
struct dl_download * dl ;
if ( ! * mime )
return NULL ;
dl = HTTP_CL_Get ( url , NULL , NotifyFunction ) ;
2016-07-12 00:40:13 +00:00
if ( dl )
{
Q_strncpyz ( dl - > postmimetype , mime , sizeof ( dl - > postmimetype ) ) ;
dl - > postdata = BZ_Malloc ( datalen ) ;
memcpy ( dl - > postdata , data , datalen ) ;
dl - > postlen = datalen ;
}
2013-10-08 14:28:11 +00:00
return dl ;
}
2010-03-14 14:35:56 +00:00
void HTTP_CL_Think ( void )
{
2013-03-12 23:09:25 +00:00
struct dl_download * dl = activedownloads ;
2010-03-14 14:35:56 +00:00
struct dl_download * * link = NULL ;
2016-07-12 00:40:13 +00:00
# ifndef SERVERONLY
float currenttime ;
if ( ! activedownloads )
return ;
currenttime = Sys_DoubleTime ( ) ;
# endif
2010-03-14 14:35:56 +00:00
link = & activedownloads ;
while ( * link )
{
2013-03-12 23:09:25 +00:00
dl = * link ;
2012-11-27 03:23:19 +00:00
# ifdef MULTITHREAD
2013-03-12 23:09:25 +00:00
if ( dl - > threadctx )
2012-11-27 03:23:19 +00:00
{
2013-03-12 23:09:25 +00:00
if ( dl - > status = = DL_FINISHED | | dl - > status = = DL_FAILED )
2012-11-27 03:23:19 +00:00
{
2013-03-12 23:09:25 +00:00
Sys_WaitOnThread ( dl - > threadctx ) ;
dl - > threadctx = NULL ;
2012-11-27 03:23:19 +00:00
continue ;
}
}
2016-07-12 00:40:13 +00:00
else if ( dl - > threadenable )
{
if ( dlthreads < MAXDOWNLOADTHREADS )
{
dl - > threadctx = Sys_CreateThread ( " download " , DL_Thread_Work , dl , THREADP_NORMAL , 0 ) ;
if ( dl - > threadctx )
dlthreads + + ;
else
dl - > threadenable = false ;
}
}
else
2012-11-27 03:23:19 +00:00
# endif
2010-03-14 14:35:56 +00:00
{
2016-07-12 00:40:13 +00:00
if ( ! dl - > poll ( dl ) )
{
* link = dl - > next ;
DL_Close ( dl ) ;
continue ;
}
2010-03-14 14:35:56 +00:00
}
2013-03-12 23:09:25 +00:00
link = & dl - > next ;
2010-03-14 14:35:56 +00:00
2015-05-16 15:51:39 +00:00
# ifndef SERVERONLY
2015-04-21 04:12:00 +00:00
if ( ! cls . download & & ! dl - > isquery )
2016-07-12 00:40:13 +00:00
# ifdef MULTITHREAD
if ( ! dl - > threadenable | | dl - > threadctx ) //don't show pending downloads in preference to active ones.
# endif
2010-03-14 14:35:56 +00:00
{
2014-06-12 23:08:42 +00:00
cls . download = & dl - > qdownload ;
dl - > qdownload . method = DL_HTTP ;
2013-03-12 23:09:25 +00:00
if ( * dl - > localname )
2014-09-08 23:47:19 +00:00
Q_strncpyz ( dl - > qdownload . localname , dl - > localname , sizeof ( dl - > qdownload . localname ) ) ;
2013-03-12 23:09:25 +00:00
else
2014-09-08 23:47:19 +00:00
Q_strncpyz ( dl - > qdownload . localname , dl - > url , sizeof ( dl - > qdownload . localname ) ) ;
Q_strncpyz ( dl - > qdownload . remotename , dl - > url , sizeof ( dl - > qdownload . remotename ) ) ;
2014-06-12 23:08:42 +00:00
dl - > qdownload . starttime = Sys_DoubleTime ( ) ;
2010-03-14 14:35:56 +00:00
}
2014-06-12 23:08:42 +00:00
if ( dl - > status = = DL_FINISHED )
dl - > qdownload . percent = 100 ;
else if ( dl - > status ! = DL_ACTIVE )
dl - > qdownload . percent = 0 ;
else if ( dl - > totalsize < = 0 )
2015-01-21 18:18:37 +00:00
{
dl - > qdownload . sizeunknown = true ;
2014-06-12 23:08:42 +00:00
dl - > qdownload . percent = 50 ;
2015-01-21 18:18:37 +00:00
}
2014-06-12 23:08:42 +00:00
else
dl - > qdownload . percent = dl - > completed * 100.0f / dl - > totalsize ;
dl - > qdownload . completedbytes = dl - > completed ;
2014-12-29 02:35:10 +00:00
2016-07-12 00:40:13 +00:00
if ( dl - > qdownload . ratetime < currenttime )
2014-12-29 02:35:10 +00:00
{
2016-07-12 00:40:13 +00:00
dl - > qdownload . ratetime = currenttime + 1 ;
2014-12-29 02:35:10 +00:00
dl - > qdownload . rate = ( dl - > qdownload . completedbytes - dl - > qdownload . ratebytes ) / 1 ;
dl - > qdownload . ratebytes = dl - > qdownload . completedbytes ;
}
2015-05-16 15:51:39 +00:00
# endif
2010-03-14 14:35:56 +00:00
}
}
2015-06-04 06:15:14 +00:00
void HTTP_CL_Terminate ( void )
{
struct dl_download * dl = activedownloads ;
struct dl_download * next = NULL ;
next = activedownloads ;
activedownloads = NULL ;
while ( next )
{
dl = next ;
next = dl - > next ;
DL_Close ( dl ) ;
}
HTTP_CL_Think ( ) ;
2015-08-24 21:44:22 +00:00
# ifdef COOKIECOOKIECOOKIE
Cookie_Monster ( ) ;
# endif
2015-06-04 06:15:14 +00:00
}
2012-11-27 03:23:19 +00:00
# endif
2010-03-14 14:35:56 +00:00
# endif /*WEBCLIENT*/
2013-11-28 00:45:22 +00:00
typedef struct
{
vfsfile_t funcs ;
char * data ;
int maxlen ;
int writepos ;
int readpos ;
2016-07-12 00:40:13 +00:00
void * mutex ;
2013-11-28 00:45:22 +00:00
} vfspipe_t ;
2014-03-30 08:55:06 +00:00
static qboolean QDECL VFSPIPE_Close ( vfsfile_t * f )
2013-11-28 00:45:22 +00:00
{
vfspipe_t * p = ( vfspipe_t * ) f ;
free ( p - > data ) ;
2016-07-12 00:40:13 +00:00
Sys_DestroyMutex ( p - > mutex ) ;
2013-11-28 00:45:22 +00:00
free ( p ) ;
2014-03-30 08:55:06 +00:00
return true ;
2013-11-28 00:45:22 +00:00
}
2014-03-30 08:55:06 +00:00
static qofs_t QDECL VFSPIPE_GetLen ( vfsfile_t * f )
2013-11-28 00:45:22 +00:00
{
vfspipe_t * p = ( vfspipe_t * ) f ;
return p - > writepos - p - > readpos ;
}
2014-03-30 08:55:06 +00:00
//static unsigned long QDECL VFSPIPE_Tell(vfsfile_t *f)
2013-11-28 00:45:22 +00:00
//{
// return 0;
//}
2014-03-30 08:55:06 +00:00
//static qboolean QDECL VFSPIPE_Seek(vfsfile_t *f, unsigned long offset)
2013-11-28 00:45:22 +00:00
//{
// Con_Printf("Seeking is a bad plan, mmkay?\n");
// return false;
//}
2014-03-30 08:55:06 +00:00
static int QDECL VFSPIPE_ReadBytes ( vfsfile_t * f , void * buffer , int len )
2013-11-28 00:45:22 +00:00
{
vfspipe_t * p = ( vfspipe_t * ) f ;
2016-07-12 00:40:13 +00:00
Sys_LockMutex ( p - > mutex ) ;
2013-11-28 00:45:22 +00:00
if ( len > p - > writepos - p - > readpos )
len = p - > writepos - p - > readpos ;
memcpy ( buffer , p - > data + p - > readpos , len ) ;
p - > readpos + = len ;
if ( p - > readpos > 8192 )
{
//shift the memory down periodically
//fixme: use cyclic buffer? max size, etc?
memmove ( p - > data , p - > data + p - > readpos , p - > writepos - p - > readpos ) ;
p - > writepos - = p - > readpos ;
p - > readpos = 0 ;
}
2016-07-12 00:40:13 +00:00
Sys_UnlockMutex ( p - > mutex ) ;
2013-11-28 00:45:22 +00:00
return len ;
}
2014-03-30 08:55:06 +00:00
static int QDECL VFSPIPE_WriteBytes ( vfsfile_t * f , const void * buffer , int len )
2013-11-28 00:45:22 +00:00
{
vfspipe_t * p = ( vfspipe_t * ) f ;
2016-07-12 00:40:13 +00:00
Sys_LockMutex ( p - > mutex ) ;
2013-11-28 00:45:22 +00:00
if ( p - > writepos + len > p - > maxlen )
{
p - > maxlen = p - > writepos + len ;
2016-07-12 00:40:13 +00:00
if ( p - > maxlen < ( p - > writepos - p - > readpos ) * 2 ) //over-allocate a little
p - > maxlen = ( p - > writepos - p - > readpos ) * 2 ;
2013-11-28 00:45:22 +00:00
p - > data = realloc ( p - > data , p - > maxlen ) ;
}
memcpy ( p - > data + p - > writepos , buffer , len ) ;
p - > writepos + = len ;
2016-07-12 00:40:13 +00:00
Sys_UnlockMutex ( p - > mutex ) ;
2013-11-28 00:45:22 +00:00
return len ;
}
vfsfile_t * VFSPIPE_Open ( void )
{
vfspipe_t * newf ;
newf = malloc ( sizeof ( * newf ) ) ;
2016-07-12 00:40:13 +00:00
newf - > mutex = Sys_CreateMutex ( ) ;
2013-11-28 00:45:22 +00:00
newf - > data = NULL ;
newf - > maxlen = 0 ;
newf - > readpos = 0 ;
newf - > writepos = 0 ;
newf - > funcs . Close = VFSPIPE_Close ;
newf - > funcs . Flush = NULL ;
newf - > funcs . GetLen = VFSPIPE_GetLen ;
newf - > funcs . ReadBytes = VFSPIPE_ReadBytes ;
newf - > funcs . Seek = NULL ; //VFSPIPE_Seek;
newf - > funcs . Tell = NULL ; //VFSPIPE_Tell;
newf - > funcs . WriteBytes = VFSPIPE_WriteBytes ;
newf - > funcs . seekingisabadplan = true ;
return & newf - > funcs ;
}