//http 断点下载
#include "http_filedownload.h"
#include "http_portinglayer.h"
#include "hash_md5.h"
#define MAX_HOST_NAME 64
#define MAX_FILE_SIZE 11
#define MAX_REQU_METH 11
/*************************************静态变量声明*****************************************/
char g_req_buf[MAX_BUFF_LEN+512];//数据暂存区
int resumePos=0,endPos=0;
int fileLength=0;
/*************************************函数功能实现*****************************************/
/*
功能:HS_Get_Breakpoint() 获得下载文件的断点;
参数:
返回:
>0---------成功
-1--------失败
*/
int HS_Get_Breakpoint(char *url,char *filename)
{
char filename_bp[MAX_FILE_NAME+3]={0};
HS_HTTP_DownloadInfo_ST *break_point;
int downloadSize=0;
int ret=0;
//是否存在断点
break_point = (HS_HTTP_DownloadInfo_ST *)HS_Malloc(sizeof(HS_HTTP_DownloadInfo_ST));
if(break_point == NULL)
{
HSPRINTF("fail to malloc memory!\n");
HS_Free(break_point);
return -1;
}
HS_Memset(break_point,0x0,sizeof(HS_HTTP_DownloadInfo_ST));
HS_Sprintf(filename_bp,"%s.bp",filename,NULL);
ret = HS_File_Read(filename_bp,break_point,sizeof(HS_HTTP_DownloadInfo_ST));
if(-1 == ret)
{
HSPRINTF("HS_File_Read error!\n");
HS_Free(break_point);
return -1;
}
#ifdef DEBUG_HTTP
HSPRINTF("breakpoint->fileurl=[%s]\n",break_point->fileurl);
HSPRINTF("breakpoint->filename=[%s]\n",break_point->filename);
HSPRINTF("breakpoint->download_size=[%d]\n",break_point->download_size);
#endif
if((HS_Memcmp(break_point->fileurl,url,HS_Strlen(url)) == 0)&&(HS_Memcmp(break_point->filename,filename,HS_Strlen(filename)) == 0))
{
downloadSize=break_point->download_size;
HS_Free(break_point);
return downloadSize;
}
else
{
HSPRINTF("%s not available\n",filename_bp);
HS_Free(break_point);
return -1;
}
}
/*
功能:HS_Save_Breakpoint() 保存下载文件的断点;
参数:
返回:
0---------成功
-1--------失败
*/
int HS_Save_Breakpoint(char *url,char *filename)
{
HS_HTTP_DownloadInfo_ST *breakpoint;
char filename_bp[MAX_FILE_NAME+3]={0};
int ret=0;
breakpoint = (HS_HTTP_DownloadInfo_ST *)HS_Malloc(sizeof(HS_HTTP_DownloadInfo_ST));
if(breakpoint == NULL)
{
HSPRINTF("fail to malloc memory!\n");
HS_Free(breakpoint);
return -1;
}
HS_Memset(breakpoint,0x0,sizeof(HS_HTTP_DownloadInfo_ST));
HS_Strcpy(breakpoint->fileurl,url);
HS_Strcpy(breakpoint->filename,filename);
breakpoint->download_size = resumePos;
HS_Sprintf(filename_bp,"%s.bp",filename,NULL);
ret = HS_File_Write(filename_bp,breakpoint,sizeof(HS_HTTP_DownloadInfo_ST));
if(-1 == ret)
{
HSPRINTF("HS_File_Write error!\n");
HS_Free(breakpoint);
return -1;
}
#ifdef DEBUG_HTTP
HSPRINTF("breakpoint->fileurl=[%s]\n",breakpoint->fileurl);
HSPRINTF("breakpoint->filename=[%s]\n",breakpoint->filename);
HSPRINTF("breakpoint->download_size=[%d]\n",breakpoint->download_size);
#endif
HS_Free(breakpoint);
return 0;
}
/*
功能:HS_HTTP_ParseURL()分析url得到Server ip,port和下载路径path;
参数:
返回:
0---------成功
-1--------失败
*/
//http://192.168.10.100:80/serv-app/tomcat-sm.rar
int HS_HTTP_ParseURL(char *url,char *ip,int *port,char *path,char *filename)
{
int ret=0;
char *tmp=NULL;
int tmplen = 0;
char host_name[MAX_HOST_NAME];
char host[32]={0};
tmp = HS_Strstr(url,"http://");
if(tmp != NULL)
{
tmplen = HS_Strlen("http://");
}
tmp = HS_Strchr(url+tmplen,'/');
if(tmp == NULL)
{
HSPRINTF("Error!!!url error:%s\n",url);
return -1;
}
else
{
//host
HS_Memset(host_name,0x0,sizeof(host_name));
HS_Memcpy(host_name,url+tmplen,HS_Strlen(url+tmplen)-HS_Strlen(tmp));
host_name[HS_Strlen(url+tmplen)-HS_Strlen(tmp)]='\0';
//path
HS_Memcpy(path,tmp,HS_Strlen(tmp)+1);
//filename
tmp = HS_Strrchr(path,'/');
if(tmp != NULL)
{
HS_Memcpy(filename,tmp+1,HS_Strlen(tmp+1)+1);
}
//get ip,port
tmp = HS_Strchr(host_name,':');
if(tmp != NULL)
{
*port=HS_Atoi(tmp+1);
HS_Memcpy(host,host_name,HS_Strlen(host_name)-HS_Strlen(tmp));
host[HS_Strlen(host_name)-HS_Strlen(tmp)]='\0';
}
else
{
*port=80;//默认端口80
HS_Memcpy(host,host_name,HS_Strlen(host_name)+1);
}
tmp=HS_Sockst_Gethostbyname_IP(host);
if (NULL == tmp)
{
HSPRINTF("gethostbyname_IP error ,host:%s\n",host);
return -1;
}
HS_Strcpy(ip,tmp);
#ifdef DEBUG_HTTP
HSPRINTF("host_name=[%s]\n",host_name);
HSPRINTF("path=[%s]\n",path);
HSPRINTF("filename=[%s]\n",filename);
HSPRINTF("ip=[%s],port=[%d]\n",ip,*port);
#endif
}
return ret;
}
/*
功能:HS_FormatRequestHeader()格式化请求头;
参数:
返回:
0---------成功
g_req_buf---返回请求头内容
*/
int HS_FormatRequestHeader(char *reqMethod,char *path,char *host,int pFrom,int pTo)
{
char tmp[MAX_FILE_SIZE];
char str[11];
HS_Memset(g_req_buf,0x0,sizeof(g_req_buf));
///第1行: 方法,请求的路径,版本
HS_Strcat(g_req_buf,reqMethod);
HS_Strcat(g_req_buf,path);
HS_Strcat(g_req_buf," HTTP/1.1");
HS_Strcat(g_req_buf,"\r\n");
///第2行:主机
HS_Strcat(g_req_buf,"Host: ");
HS_Strcat(g_req_buf,host);
HS_Strcat(g_req_buf,"\r\n");
///第3行:接收的数据类型
HS_Strcat(g_req_buf,"Accept: */*");
HS_Strcat(g_req_buf,"\r\n");
///第4行:浏览器类型
//HS_Strcat(g_req_buf,"User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
//HS_Strcat(g_req_buf,"\r\n");
///第5行:请求的数据起始字节位置(断点续传的关键)
if(pFrom >= 0)
{
HS_Strcat(g_req_buf,"Range: bytes=");
HS_Sprintf(tmp,"%d",(int *)pFrom,NULL);
HS_Strcat(g_req_buf,tmp);
HS_Strcat(g_req_buf,"-");
if(pTo > pFrom)
{
HS_Sprintf(tmp,"%d",(int *)pTo,NULL);
HS_Strcat(g_req_buf,tmp);
}
HS_Strcat(g_req_buf,"\r\n");
}
///第6行:连接设置,保持
HS_Strcat(g_req_buf,"Keep-Alive: 200");
HS_Strcat(g_req_buf,"\r\n");
HS_Strcat(g_req_buf,"Connection: Keep-Alive");
HS_Strcat(g_req_buf,"\r\n");
///最后一行:空行
HS_Strcat(g_req_buf,"\r\n");
#ifdef DEBUG_HTTP
HSPRINTF("g_req_buf=[%s]\n",g_req_buf);
#endif
return 0;
}
/*
功能:HS_GetServerState()得到回应状态码;
参数:
返回:
>0---------成功,返回状态码statusCode
-1---------失败
*/
int HS_GetServerState(char *recvBuff)
{
char *tmp=NULL;
int statusCode;
char p[4]={0};
tmp=HS_Strstr(recvBuff,"HTTP/1.1");
if(tmp != NULL)
{
HS_Memcpy(p,tmp+9,4);
statusCode=HS_Atoi(p);
}
else
{
HSPRINTF("ERROR!!!recvBuff error!");
return -1;
}
return statusCode;
}
/*
功能:HS_Recv_Response()接收数据直到数据接收完成;
参数:
返回:
>0--------服务器接收成功,返回接收的数据
-1--------失败
=0--------服务器断开连接
*/
int HS_Recv_Response(int sockfd)
{
HS_Memset(g_req_buf,0x0,sizeof(g_req_buf));
int ret=0,numbytes=0;
int contLen=0;
char *tmp=NULL;
while(1)
{
ret = HS_Sockst_Recv(sockfd, g_req_buf+numbytes, sizeof(g_req_buf), 0);
if(ret > 0)
{
numbytes+=ret;
}
else if(ret == 0)
{
return ret;
}
else
{
HSPRINTF("~~~ret=[%d],errno=[%d]\n",ret,errno);
if((ret < 0) && (errno == EINTR))//errno == EAGAIN||errno == EWOULDBLOCK||
{
continue;//继续接收数据
}
HSPRINTF("HS_Sockst_Recv error\n");
return -1;
}
if(contLen == 0)
{
tmp=HS_Strstr(g_req_buf,"Content-Length: ");
if(NULL == tmp)
{
HSPRINTF("recv header error!\n");
return -1;
}
contLen=HS_Atoi(tmp+16);
tmp=HS_Strstr(g_req_buf,"\r\n\r\n");
if(NULL == tmp)
{
HSPRINTF("recv header error!\n");
return -1;
}
HSPRINTF("#######mp-g_req_buf+4=[%d]\n",(tmp-g_req_buf+4));
}
if(numbytes == (contLen+(tmp-g_req_buf+4)))
{
#ifdef DEBUG_HTTP
HSPRINTF("numbytes=[%d],contLen=[%d]\n",numbytes,contLen);
没有合适的资源?快使用搜索试试~ 我知道了~
http.rar_HTTP_Linux C下载器_c http 下载 linux_http 文件_linux c http
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共41个文件
svn-base:27个
c:5个
h:4个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 173 浏览量
2022-09-24
07:10:25
上传
评论
收藏 125KB RAR 举报
温馨提示
c语言实现的在linux下编译的,可以通过输入地址文件名下载服务器上的文件的源码,封装了中间层,便于在不同平台上移植。
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 41 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/f3b7c8d80edb45ee84389e2d10b9d009_weixin_42662293.jpg!1)
局外狗
- 粉丝: 69
- 资源: 1万+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)