点击(此处)折叠或打开
点击(此处)折叠或打开
很早就有这个想法了,知道今天才实现了一下,参考了周立发的《《C语言实现HTTP下载例子》》和林超旗整理的《《HTTP协议详解》》,特此谢谢!由于今天时间有限,所以大概的实现了一下(目前只在ubuntu环境下测试的),跨平台部分还需要完善,后面会陆续整理出完整的版本。代码如下:
头文件
/*
$Id: httpdownload.h, v1.0.0, 2011.6.28, YellowBug $
$@@: http下载
*/
#ifndef PENQ_HTTP_DOWNLOAD_H_
#define PENQ_HTTP_DOWNLOAD_H_
#include
#include
#include
#include
#if defined(__GNUC__)
#include
#include
#include
#include
#include
#include
#include
#define PATH_SPLIT '/'
typedef int HSOCKET;
#define closesocket(s) close(s)
#elif defined(_WIN32)
#include
#include
#define PATH_SPLIT '//'
typedef SOCKET HSOCKET;
#endif
#define DEF_HTTP_PORT 80
#define HTTP_MAX_PATH 260
#define HTTP_MAX_REQUESTHEAD 1024
#define HTTP_MAX_RESPONSEHEAD 2048
#define HTTP_DOWN_PERSIZE 128
#define HTTP_FLUSH_BLOCK 1024
/*
$@@ 下载状态
$@ HDL_URLERR: 提供的url非法
$@ HDL_SERVERFAL: 根据host地址无法找到主机
$@ HDL_TIMEOUT: 连接下载地址超时
$@ HDL_READYOK: 连接下载地址成功,准备下载
$@ HDL_DOWNING: 正在下载
$@ HDL_DISCONN: 断开连接了
$@ HDL_FINISH: 下载完成
*/
enum
{
HDL_URLERR = 0xe0,
HDL_SERVERFAL = 0xe1,
HDL_SOCKFAL = 0xe2,
HDL_SNDREQERR = 0xe3,
HDL_SERVERERR = 0xe4,
HDL_CRLOCFILEFAL = 0xe5,
HDL_TIMEOUT = 0x100,
HDL_NORESPONSE = 0x101,
HDL_READYOK = 0x104,
HDL_DOWNING = 0x105,
HDL_DISCONN = 0x106,
HDL_FINISH = 0x107
};
/*
$@@ 下载回调函数,主要用于监听下载过程
$@ 第一个参数: 下载标识符号(上面枚举中的值)
$@ 第二个参数: 需要下载的总字节数
$@ 第三个参数: 已经下载的总字节数
$@ 第四个参数: 距离上一次下载的时间戳(毫秒)
$@ 返回值: 下次下载的字节数(0: 表示默认值, 小于0则下载中断, 大于0则位下次下载指定的字节)
*/
typedef int (*HTTPDOWNLOAD_CALLBACK)(int, unsigned int, unsigned int, unsigned int);
/*
$@@ 通过指定的url下载资源
$@ url: 下载资源地址
$@ filepath: 存储在本地的路径(如果为NULL则存储在当前目录)
$@ filename: 存储的文件名(如果为NULL则位url中分析的文件名)
$@ http_func: 下载过程监听函数(如果为NULL则不监听下载过程)
*/
void pq_http_download(const char *url, const char *filepath, const char *filename, HTTPDOWNLOAD_CALLBACK http_func);
#endif /* #ifndef PENQ_HTTP_DOWNLOAD_H_ */
/* END */
/*
$Id: httpdownload.c, v1.0.0, 2011.6.28, YellowBug $
$@@: http下载
*/
#include "httpdownload.h"
#define HERROR(exp, err) do { /
if (exp) { /
printf("(%s,%d) %s/n", __FILE__, __LINE__, #exp); /
if(S_http_callback) S_http_callback((err), 0, 0, 0); /
exit(1); /
} /
} while(0)
#define HCALLBACK(a, b, c, d) (S_http_callback ? S_http_callback((a), (b), (c), (d)) : 0)
/*
$@@ 文件域的局部变量
*/
static HTTPDOWNLOAD_CALLBACK S_http_callback = NULL;
static const char * S_request_head = "GET %s HTTP/1.1/r/n" /
"Accept: */*/r/n" /
"Accept-Language: zh-cn/r/n" /
"User-Agent: Molliza/4.0(compatible;MSIE6.0;windows NT 5.0)/r/n" /
"Host: %s/r/n" /
"Connection: close/r/n/r/n";
#if defined(__GNUC__)
unsigned int get_tick()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (unsigned int)(tv.tv_sec*1000+tv.tv_usec/1000);
}
#elif defined(_WIN32)
#define get_tick() GetTickCount()
#endif
/*
$@@ 从指定长度(len0)的字符串的右端开始找到第一个位ch的字符
*/
static char * r_strchr(const char *p, int len, char ch)
{
int last_pos = -1;
int i = 0;
while ( *(p+i) != '/0' ) {
if ( *(p+i) == ch ) last_pos = i;
if ( ++i >= len ) break;
}
return last_pos >= 0 ? (char *)(p+last_pos) : NULL;
}
/*
$@@ 解析url,分析出web地址,端口号,下载的资源名称
*/
static void parse_url(const char *url, char *web, int *port, char *filepath, char *filename)
{
int web_len = 0;
int web_start = 0;
int file_len = 0;
int file_start = 0;
int path_len = 0;
int path_start = 0;
char *pa = NULL;
char *pb = NULL;
if ( !strncmp(url, "http://", 7) ) web_start = 7;
else if ( !strncmp(url, "https://", 8) ) web_start = 8;
pa = strchr(url+web_start, '/');
pb = strchr(url+web_start, ':');
/* 获取web */
if ( pa ) {
web_len = (int)(pa - (url+web_start));
} else {
if ( pb ) web_len = pb - (url+web_start);
else web_len = strlen(url+web_start);
}
HERROR(0 == web_len, HDL_URLERR);
memcpy(web, url+web_start, web_len);
web[web_len] = '/0';
/* 获取filename */
if ( pb ) {
pa = r_strchr(url+web_start, (int)(pb-(url+web_start)), '/');
file_len = (int)(pb - pa - 1);
file_start = (int)(pa + 1 - url);
*port = atoi(pb+1);
} else {
pa = r_strchr(url+web_start, strlen(url+web_start), '/');
HERROR(NULL == pa, HDL_URLERR);
file_len = strlen(pa+1);
file_start = (int)(pa + 1 - url);
*port = DEF_HTTP_PORT;
}
HERROR(NULL == pa, HDL_URLERR);
memcpy(filename, url+file_start, file_len);
filename[file_len] = '/0';
/* 获取filepath */
path_start = web_start + web_len;
path_len = file_start - path_start + file_len;
memcpy(filepath, url+web_start+web_len, file_start - path_start + file_len);
filepath[path_len] = '/0';
}
/*
$@@ 封装request报头
*/
int package_request(char *request, const char *web, const char *filepath)
{
int n = sprintf(request, S_request_head, filepath, web);
request[n] = '/0';
return n;
}
/*
$@@ 发送请求报头
*/
int send_request(HSOCKET sock, const char *buf, int n)
{
int already_bytes = 0;
int per_bytes = 0;
while ( already_bytes < n ) {
per_bytes = send(sock, buf+already_bytes, n-already_bytes, 0);
if ( per_bytes < 0 ) {
break;
} else if ( 0 == per_bytes ) {
HCALLBACK(HDL_DISCONN, 0, 0, 0);
break;
} else
already_bytes += per_bytes;
}
return already_bytes;
}
/*
$@@ 获取响应报头(状态行和消息报头)
*/
int get_response_head(HSOCKET sock, char *response, int max)
{
int total_bytes = 0;
int per_bytes = 0;
int over_flag = 0;
char ch;
do {
per_bytes = recv(sock, &ch, 1, 0);
if ( per_bytes < 0 ) break;
else if ( 0 == per_bytes ) {
HCALLBACK(HDL_DISCONN, 0, 0, 0);
break;
} else {
if ( '/r' == ch || '/n' == ch ) over_flag++;
else over_flag = 0;
response[total_bytes] = ch;
if ( total_bytes >= max ) break;
else total_bytes += per_bytes;
}
} while ( over_flag < 4 );
response[total_bytes] = '/0';
return total_bytes;
}
void parse_response_head(char *response, int n, int *server_status, unsigned int *down_total)
{
int i = 1;
char *pstatus = strstr(response, "HTTP/1.1");
char *plen = strstr(response, "Content-Length:");
printf("%s/n", response);
if ( pstatus ) {
pstatus += strlen("HTTP/1.1");
i = 1;
while ( *(pstatus+i) == ' ' ) ++i;
*server_status = atoi(pstatus+i);
} else
*server_status = 0;
if ( plen ) {
plen += strlen("Content-Length:");
i = 1;
while ( *(plen+i) == ' ' ) ++i;
*down_total = (unsigned int)atoi(plen+i);
} else
*down_total = 0;
printf("status: %d/n", *server_status);
printf("total: %u/n", *down_total);
}
/*
$@@ 创建本地文件
*/
FILE * create_local_file(const char *local_path, const char *local_file, const char *remote_file)
{
int len = strlen(local_path);
static char filename[HTTP_MAX_PATH+1];
if ( local_path[len-1] != PATH_SPLIT ) {
if ( local_file ) len = sprintf(filename, "%s%c%s", local_path, PATH_SPLIT, local_file);
else len = sprintf(filename, "%s%c%s", local_path, PATH_SPLIT, remote_file);
} else {
if ( local_file ) len = sprintf(filename, "%s%s", local_path, local_file);
else len = sprintf(filename, "%s%s", local_path, remote_file);
}
filename[len] = '/0';
printf("ready to create local file: %s/n", filename);
return fopen(filename, "wb+");
}
/*
$@@ 开始下载
*/
void start_download(HSOCKET sock, unsigned int total_bytes, FILE *stream)
{
unsigned int already_bytes;
int per_bytes;
int flush_bytes;
unsigned int total_tim;
unsigned int per_tim;
static char buf[HTTP_DOWN_PERSIZE];
do {
per_tim = get_tick();
per_bytes = recv(sock, buf, HTTP_DOWN_PERSIZE, 0);
per_tim = get_tick() - per_tim;
if ( per_bytes < 0 ) break;
else if ( 0 == per_bytes ) {
HCALLBACK(HDL_DISCONN, total_bytes, already_bytes, 0);
break;
} else {
fwrite(buf, 1, per_bytes, stream);
already_bytes += (unsigned int)per_bytes;
flush_bytes += per_bytes;
if ( flush_bytes >= HTTP_FLUSH_BLOCK ) {
fflush(stream);
flush_bytes = 0;
}
HCALLBACK(HDL_DOWNING, total_bytes, already_bytes, per_tim);
}
} while ( already_bytes < total_bytes );
if ( flush_bytes > 0 )
fflush(stream);
HCALLBACK(HDL_FINISH, total_bytes, already_bytes, total_tim);
}
void pq_http_download(const char *url, const char *local_path, const char *local_file, HTTPDOWNLOAD_CALLBACK http_func)
{
int port;
char web[HTTP_MAX_PATH+1];
char remote_path[HTTP_MAX_PATH+1];
char remote_file[HTTP_MAX_PATH+1];
char request[HTTP_MAX_REQUESTHEAD+1];
char response_head[HTTP_MAX_RESPONSEHEAD+1];
int request_len = 0;
int response_len = 0;
unsigned int down_total = 0;
int server_status = 404;
HSOCKET sock = -1;
struct hostent *host = NULL;
struct sockaddr_in server_addr;
FILE *stream = NULL;
S_http_callback = http_func;
parse_url(url, web, &port, remote_path, remote_file);
request_len = package_request(request, web, remote_path);
host = gethostbyname(web);
HERROR(NULL==host, HDL_SERVERFAL);
sock = socket(AF_INET, SOCK_STREAM, 0);
HERROR(-1==sock, HDL_SOCKFAL);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
HERROR(-1==connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)), HDL_TIMEOUT);
HERROR(request_len != send_request(sock, request, request_len), HDL_SNDREQERR);
response_len = get_response_head(sock, response_head, HTTP_MAX_RESPONSEHEAD);
HERROR(response_len <= 0, HDL_NORESPONSE);
parse_response_head(response_head, response_len, &server_status, &down_total);
switch ( server_status ) {
case 200:
if ( down_total > 0 ) {
stream = create_local_file(local_path, local_file, remote_file);
HERROR(NULL == stream, HDL_CRLOCFILEFAL);
HCALLBACK(HDL_READYOK, down_total, 0, 0);
start_download(sock, down_total, stream);
fclose(stream);
} else {
HCALLBACK(HDL_FINISH, 0, 0, 0);
}
default:
HCALLBACK(HDL_SERVERERR, 0, 0, 0);
}
closesocket(sock);
}
/* END */
#include
#include "httpdownload.h"
int http_callback(int status, unsigned int total_bytes, unsigned int already_bytes, unsigned int tim)
{
switch ( status ) {
case HDL_READYOK:
printf("ready to download, total.bytes=%u/n", total_bytes);
break;
case HDL_DOWNING:
printf("downloading... (%u/%u) bytes/n", already_bytes, total_bytes);
break;
case HDL_FINISH:
printf("download finish! download total.bytes=%u/n", already_bytes);
break;
default:
printf("status: %#x/n", status);
}
return 0;
}
int main(int argc, char *argv[])
{
pq_http_download(argv[1], argv[2], NULL, http_callback);
}
一个http下载程序的c实现
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define RECVBUFSIZE 2048
#define READBUFSIZE 2048
/********************************************
功能:搜索字符串右边起的第一个匹配字符
********************************************/
char * Rstrchr(char * s, char x) {
int i = strlen(s);
if(!(*s)) return 0;
while(s[i-1]) if(strchr(s + (i - 1), x)) return (s + (i - 1)); else i--;
return 0;
}
/********************************************
功能:把字符串转换为全小写
********************************************/
void ToLowerCase(char *s) {
while(*s) {
*s=tolower(*s);
s++;
}//while
}
/**************************************************************
功能:从字符串src中分析出网站地址和端口,并得到用户要下载的文件
***************************************************************/
void GetHost(char * src, char * web, char * file, int * port) {
char * pA;
char * pB;
memset(web, 0, sizeof(web));
memset(file, 0, sizeof(file));
*port = 0;
if(!(*src)) return;
pA = src;
if(!strncmp(pA, "http://", strlen("http://"))) pA = src+strlen("http://");
else if(!strncmp(pA, "https://", strlen("https://"))) pA = src+strlen("https://");
pB = strchr(pA, '/');
if(pB) {
memcpy(web, pA, strlen(pA) - strlen(pB));
if(pB+1) {
memcpy(file, pB + 1, strlen(pB) - 1);
file[strlen(pB) - 1] = 0;
}
}
else memcpy(web, pA, strlen(pA));
if(pB) web[strlen(pA) - strlen(pB)] = 0;
else web[strlen(pA)] = 0;
pA = strchr(web, ':');
if(pA) *port = atoi(pA + 1);
else *port = 80;
}
int find_start(char *buf, int size)
{
int i;
char *p;
p = strstr(buf, "\r\n\r\n");
if(p) {
i = p - buf + 4;
if(i >= size)
i = -1;
}
else
i = -1;
return i;
}
//唯一的参数就是网址网址
int main(int argc, char **argv) {
int sockfd;
int fd;
struct hostent *host;
int portnumber;
char url[128];
char host_addr[256];
char host_file[1024];
char request[1024];
struct sockaddr_in server_sockaddr;
char recvbuf[RECVBUFSIZE];
int sendbytes, recvbytes, totalsend, totalsended;
int file_start_pos;
sprintf(url, "%s", argv[1]);
ToLowerCase(url);
GetHost(url, host_addr, host_file, &portnumber);
host = gethostbyname(host_addr);
sockfd = socket(PF_INET, SOCK_STREAM, 0);
server_sockaddr.sin_family = PF_INET;
server_sockaddr.sin_port = htons(portnumber);
server_sockaddr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_sockaddr.sin_zero), 8);
connect(sockfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr));
sprintf(request, "GET /%s HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\n\
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\
Host: %s:%d\r\nConnection: Keep-Alive\r\n\r\n", host_file, host_addr, portnumber);
sendbytes = 0;
totalsended = 0; //already send
totalsend = strlen(request); //will send
while(totalsended < totalsend) {
sendbytes = send(sockfd, request+totalsended, totalsend - totalsended, 0);
totalsended += sendbytes;
}//while
fd = open("download", O_CREAT | O_TRUNC | O_RDWR, 0777);
recvbytes = recv(sockfd, recvbuf, RECVBUFSIZE, 0); //delete the head
file_start_pos = find_start(recvbuf, recvbytes);
write(fd, recvbuf+file_start_pos, recvbytes-file_start_pos);
while((recvbytes = recv(sockfd, recvbuf, RECVBUFSIZE, 0)) > 0) {
write(fd, recvbuf, recvbytes);
}//while
close(fd);
close(sockfd);
return 0;
}//main()