📄 http.c
字号:
/* http.c
*
* Copyright (C) 2004-2004 Wang Xiaoguang (Chice) <chice_wxg@hotmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* Authors: Wang Xiaoguang (Chice) <chice_wxg@hotmail.com>
*/
#include "http.h"#include "../dld_log.h"
#include "../dld_language.h"
#include "../dld_config.h"
#include "../dld_task.h"
#include "../dld_taskmgr.h"
#include "../dld_thread.h"
#include "../dld_proxymgr.h"
#include "../dld_proxy.h"
#include "../base64.h"
#include "../dld_ui_callback.h"
/////////////////////////////////
//HTTP Protocol Structs
/////////////////////////////////
enum
{
HTTP_STATUS_WAITING = 0,
HTTP_STATUS_WAIT_FOR_HEADER,
HTTP_STATUS_WAIT_FOR_DATA
};
typedef struct _HttpProtocolData
{
INT dwTmp;
}HttpProtocolData;
typedef struct _HttpTaskThread
{
//Public Member ... (The same as other TaskThread)
DLD_TASKTHREAD_PUBLICMEMBER;
//Private Member
INT cmdstatus;
SOCKET connsocket;
BUFFER *buffer;
}HttpTaskThread;
THREADRET http_thread_download(POINTER param);
THREADRET http_thread_main(POINTER param);DldTaskThread *http_thread_new(DldTask *task);//init the data of HTTP protocol ( task->protocoldata )// NOTHING TO DO NOW!!!!!!!!INT http_protocol_data_init(DldTask *task){ HttpProtocolData *data = NULL; data = os_new(HttpProtocolData, 1); task->protocoldata = data; return DLD_ERR_OK;}//destroy the data of HTTP protocol// free finishedblocks(LIST, HTTP_DATA_BLOCK)// free protocoldataINT http_protocol_data_destroy(DldTask *task){ HttpProtocolData *data; data = task->protocoldata; //Do Some Free here! task->protocoldata = os_free(task->protocoldata); return DLD_ERR_OK;}
DldTaskThread *http_taskthread_new(DldTask *task)
{
HttpTaskThread *taskthread;
taskthread = os_new(HttpTaskThread, 1);
taskthread->protocoltype = DLD_PROTOCOL_HTTP;
taskthread->status = DLD_TASKTHREAD_STOPPED;
taskthread->parenttask = task;
taskthread->block = NULL;
taskthread->fp = NULL;
taskthread->timeouttick = os_tick_new(g_config.timeout, FALSE);
taskthread->retrytick = os_tick_new(g_config.retrydelay, FALSE);
taskthread->log = dld_log_new();
taskthread->cmdstatus = HTTP_STATUS_WAITING;
taskthread->connsocket = 0;
taskthread->buffer = buffer_new(4096);
return (DldTaskThread *)taskthread;
}
DldTaskThread *http_taskthread_free(DldTaskThread *dldtaskthread)
{
HttpTaskThread *taskthread = (HttpTaskThread *)dldtaskthread;
if(!taskthread) return NULL;
taskthread->block = os_free(taskthread->block);
if(taskthread->fp)
{
fclose(taskthread->fp);
taskthread->fp = NULL;
}
taskthread->log = dld_log_free(taskthread->log);
taskthread->parenttask = NULL;
taskthread->timeouttick = os_tick_free(taskthread->timeouttick);
taskthread->retrytick = os_tick_free(taskthread->retrytick);
if(taskthread->connsocket)
{
os_socket_tcp_close(taskthread->connsocket);
taskthread->connsocket = 0;
}
taskthread->buffer = buffer_free(taskthread->buffer);
//taskthread = os_free(taskthreaddata);
taskthread = os_free(taskthread);
return NULL;
}
VOID http_task_free_taskthreads(DldTask *task)
{
if(!task) return;
while(task->taskthreads)
{
http_taskthread_free((DldTaskThread *)(task->taskthreads->data));
task->taskthreads = list_remove_nth(task->taskthreads, 0);
}
}
INT http_taskthread_start(DldTaskThread *dldtaskthread)
{
DldTask *task;
HttpTaskThread *taskthread;
//HttpTaskThreadData *taskthreaddata;
taskthread = (HttpTaskThread *)dldtaskthread;
task = (DldTask *)taskthread->parenttask;
//if(taskthreaddata->protocoltype != DLD_PROTOCOL_HTTP)
taskthread->status = DLD_TASKTHREAD_RUNNING;
if(!taskthread->fp) taskthread->fp = fopen(task->localfile->str, "rb+");
if(!taskthread->fp) taskthread->fp = fopen(task->localfile->str, "wb+");
if(taskthread->connsocket != 0)
{
os_socket_tcp_close(taskthread->connsocket);
taskthread->connsocket = 0;
}
taskthread->buffer = buffer_clear(taskthread->buffer);
taskthread->cmdstatus = HTTP_STATUS_WAITING;
return DLD_ERR_OK;
}
INT http_taskthread_stop(DldTaskThread *dldtaskthread)
{
HttpTaskThread *taskthread = (HttpTaskThread *) dldtaskthread;
taskthread->status = DLD_TASKTHREAD_STOPPED;
if(taskthread->fp)
{
fclose(taskthread->fp);
taskthread->fp = NULL;
}
if(taskthread->connsocket != 0)
{
os_socket_tcp_close(taskthread->connsocket);
taskthread->connsocket = 0;
}
taskthread->block = os_free(taskthread->block);
return DLD_ERR_OK;
}
VOID http_taskthread_meet_error(HttpTaskThread *taskthread, BOOL replyerr)
{
STRING *reply;
dld_taskthread_add_log((DldTaskThread *)taskthread, _("HTTP connection meets an error."));
if(replyerr)
{
if(taskthread->buffer->len >= 4)
{
reply = string_nappend(NULL, (char *)taskthread->buffer->data, taskthread->buffer->len);
dld_taskthread_add_log((DldTaskThread *)taskthread, _("Server reply : \r\n%s\r\n"), reply->str);
reply = string_free(reply);
}
http_taskthread_stop((DldTaskThread *)taskthread); //stop it!
if(taskthread->parenttask->filesize == 0 && dld_taskthread_is_first((DldTaskThread *)taskthread))
{
//Stop the task by ERROR!
taskthread->parenttask->status = DLD_TASK_ERROR;
}
}
if(!replyerr)
{
if(taskthread->status == HTTP_STATUS_WAITING)
{
dld_taskthread_add_log((DldTaskThread *)taskthread, _("Can not connect to remote host. Invalid Host or Too Many Connections.\n"));
}
else
{
dld_taskthread_add_log((DldTaskThread *)taskthread, _("Your network may not work correctly. Retry...\n"));
}
//retry...
taskthread->retries ++;
if(taskthread->retries <= g_config.maxretries)
{
os_tick_clear(taskthread->retrytick);
http_taskthread_start((DldTaskThread *)taskthread);
}
else
{
http_taskthread_stop((DldTaskThread *)taskthread);
dld_taskthread_add_log((DldTaskThread *)taskthread, _("Can not connect to remote host. Invalid Host or Too Many Connections.\n"));
}
}
if(dld_task_get_runningthreadnumber(taskthread->parenttask) == 0)
{
taskthread->parenttask->status = DLD_TASK_ERROR;
dld_ui_callback(DLD_UICMD_TASK_STATUS, taskthread->parenttask);
}
}
//make a request for downloading fileSTRING *http_request_make(DldTask *task, UINT start, UINT end){ CHAR tmp[100]; STRING *request; CHAR *p;
DldProxy *proxy;
CHAR *host, *username, *password;
//if(dld_proxy_get_protocoltype(proxy) != DLD_PROTOCOL_PROXY_UNKNOWN)
proxy = dld_proxymgr_get_proxy(task->proxy->str);
request = string_new(""); //GET file HTTP/1.1 request = string_append(request, (start == 0 && end == 0) ? "HEAD " : "GET ");
if(dld_proxy_get_protocoltype(proxy) == DLD_PROTOCOL_PROXY_HTTP_GET)
{
request = string_append(request, task->protocol->str);
request = string_append(request, "://");
if(strlen(task->username->str) != 0 && strlen(task->password->str) != 0)
{
request = string_append(request, task->username->str);
request = string_append(request, ":");
request = string_append(request, task->password->str);
request = string_append(request, "@");
}
request = string_append(request, task->host->str);
request = string_append(request, ":");
request = string_append(request, task->port->str);
request = string_append(request, task->remotefile->str);
host = proxy->host->str;
username = proxy->username->str;
password = proxy->password->str;
}
else
{ request = string_append(request, task->remotefile->str);
host = task->host->str;
username = task->username->str;
password = task->password->str;
}
request = string_append(request, " HTTP/1.1\r\n"); //HOST: host request = string_append(request, "Host: ");
request = string_append(request, host);
request = string_append(request, "\r\n");
if(strlen(username) != 0 && strlen(password) != 0)
{
snprintf(tmp, sizeof(tmp), "%s:%s", username, password);
p = base64_encode_string(tmp);
request = string_append(request, "Authorization: Basic ");
request = string_append(request, p);
request = string_append(request, "\r\n");
base64_free_string(p);
}
//Connection: close request = string_append(request, "Connection: close\r\n"); //Range: bytes=START-END if( (start != 0 || end != 0) ) { snprintf(tmp, sizeof(tmp), "Range: bytes=%d-\r\n", start); request = string_append(request, tmp); } request = string_append(request, "\r\n");
//TODO
// add reference
dprintf("[HTTP REQUEST]\n%s\n", request->str); return request;}//get HTTP code . eg. HTTP/1.1 206 INT http_header_get_code(BUFFER *buf)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -