c语⾔实现http下载器的⽅法⼀、介绍
最近做ota升级需要⽤到http下载,所以写了⼀下http下载器
实现流程
1、解析url⽹址的域名和⽂件名
2、获取ip地址
3、构建http请求头发送到服务器
4、解析回复
5、下载⽂件
环境ubuntu linux
c语⾔
main.c
#include <stdio.h>
#include "http_download.h"
int main(int argc, char const *argv[])
{
if (argc == 1)
{
printf("Input a valid URL \n");
exit(0);
}
else
{
http_download_file(argv[1]);
}
return 0;
}
http_download.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include "tcp.h"
typedef struct {
int status_code;//HTTP/1.1 '200' OK
char content_type[128];//Content-Type: application/gzip
long content_length;//Content-Length: 11683079
char file_name[256];
}resp_header_def;
resp_header_def resp;//头信息
#if 0
下载分为以下⼏个过程
1、解析出下载地址中的域名和⽂件名
2、通过域名获取服务器的IP地址
3、与⽬标服务器建⽴连接
4、构建http请求头并将其发送到服务器
5、等待服务器响应然后接收响应头
6、解析响应头, 判断返回码, 分离开响应头, 并且响应的正⽂内容以字节形式写⼊⽂件, 正⽂内容与头部⽤两个\n\r分开#endif
#define print_LOG(format, ...)    {\
printf(format, ##__VA_ARGS__);}
c语言下载什么//解析⽹址
//输⼊url,输出域名、端⼝、⽂件名
static int Parsing_urls(char *url, char *domain, int *port, char *filename)
{
int i,j,start;
char *patterns[] = {"", "", NULL};
*port = 80;
//解析域名,就是或者到/的内容
for(i = 0; patterns[i]; i++)
{
if(strncmp(url, patterns[i], strlen(patterns[i])) == 0)
{
start = strlen(patterns[i]);
}
}
//复制域名
j = 0;
for ( i = start; url[i] != '/' && url[i] != '\0'; i++,j++)
{
domain[j] = url[i];
}
domain[i] = '\0';
//解析端⼝,冒号后⾯就是端⼝
char pos = strstr(domain, ":");
if(pos)
{
sscanf(pos, ":%d", port);
}
//如果有端⼝,需要删掉
for ( i = 0; i < (int)strlen(domain); i++)
{
if(domain[i] == ':')
{
domain[i] = '\0';
break;
}
}
//解析下载⽂件名,/后⾯就是⽂件名
j = 0;
for ( i = start; url[i] != '\0'; i++)
{
if(url[i] == '/')
{
j = i + 1;
memcpy(filename, &url[j], strlen(&url[j]));
}
}
i = strlen(&url[j]);
filename[i] = '\0';
return 0;
}
//通过域名获取ip
static int Domain_to_ip(char *domain, char *ip)
{
int i;
struct hostent *host = gethostbyname(domain);
if(host == 0)
{
*ip = NULL;
return -1;
}
//到不为空的地址
for ( i = 0; host->h_addr_list[i]; i++)
{
strcpy(ip, inet_ntoa( * (struct in_addr*) host->h_addr_list[i]));
break;
}
return 0;
}
/
/构建请求头信息
static int Set_request_headers(char *header, char *url, char *domain)
{
int ret = 0;
sprintf(header, \
"GET %s HTTP/1.1\r\n"\
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"\
"User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537(KHTML, like Gecko) Chrome/47.0.2526Safari/537.36\r\n"\            "Host:%s\r\n"\
"Connection:close\r\n"\
"\r\n"\
,url, domain);
ret = strlen(header);
return ret;
}
static char *get_response()
{
int len, lencnt  = 0,mem_size = 4096;
char *rcvbuff = (char *)malloc( mem_size*sizeof(char) );
char *response = (char *)malloc( mem_size*sizeof(char) );
while((len = tcp_client_rcv(rcvbuff, 1)) != 0)
{
//动态调整缓存⼤⼩
if((lencnt + len) > mem_size)//判断缓存是否超限
{
//重新分配内存
mem_size *= 2;
char *tmp = (char *)realloc(response, mem_size*sizeof(char));            if(tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
response = tmp;
}
rcvbuff[len] = '\0';
strcat(response, rcvbuff);
//到响应头的头部信息, 两个"\n\r"为分割点
int flag = 0;
int i = strlen(response) - 1;
for (; response[i] == '\n' || response[i] == '\r';
i--, flag++);
{
if (flag == 4)//最多4次,没到
break;
}
lencnt += len;
}
free(rcvbuff);
return response;
}
//获取回复头的信息
static int get_resp_header(const char *response, resp_header_def *resp) {
//查HTTP/
char *pos = strstr(response, "HTTP/");
if (pos)
sscanf(pos, "%*s %d", &resp->status_code);//返回状态码
pos = strstr(response, "Content-Type:");//返回内容类型
if (pos)
sscanf(pos, "%*s %s", resp->content_type);
pos = strstr(response, "Content-Length:");//内容的长度(字节)
if (pos)
sscanf(pos, "%*s %ld", &resp->content_length);
return 0;
}
//打印进度
int progress_bar(int x)
{
int i;
char tmp[100] = {0};
static int x_old = 0;
if(x == x_old)
{
return 0;
}
x_old = x;
i = x/2;
if(i > 50)
i = 50;
memset(tmp, '=', i);
printf("\r%d%[%s]", x, tmp);
fflush(stdout);//⽴刻输出
return 0;
}
static int download_writefile()
{
int length = 0;
int mem_size = 4096;//mem_size might be enlarge, so reset it
int buf_len = mem_size;//read 4k each time
int len = 0;
int fd = open(resp.file_name, O_CREAT | O_WRONLY, S_IRWXG | S_IRWXO | S_IRWXU);    if (fd < 0)
{
print_LOG("Create file failed\n");
exit(0);
}
//申请4k缓存
char *buf = (char *)malloc(mem_size * sizeof(char));
//读取⽂件
while ((len = tcp_client_rcv(buf, buf_len)) != 0 && length < t_length)
{
write(fd, buf, len);
length += len;
progress_bar((length*t_length));
}
if (length == t_length)
{
print_LOG("\nDownload successful ^_^\n\n");
}
else
{
print_LOG("Finished length:%t_length:%d\n",
length, t_length);
}
close(fd);
return 0;
}
//http下载⽂件函数
int http_download_file(char *url)
{
//char url[2048] = "127.0.0.1";
char domain[64] = {0};
char ip_addr[16] = {0};
int port = 80;
char file_name[256] = {0};
char header[2048] = {0};
char *response = 0;
//解析域名
Parsing_urls(url, domain, &port, file_name);
//解析ip
Domain_to_ip(domain, ip_addr);
print_LOG("download:\n url:%s\n domain:%s\n ip:%s\n port:%d\n filename:%s\n",
url, domain, ip_addr, port, file_name);
//发送下载请求
Set_request_headers(header, url, domain);
if(tcp_client_init(ip_addr,  port) == -1)
{
print_LOG("connect server fail\n");
return -1;
}
tcp_client_send(header, strlen(header));
print_LOG("\nsend request\n");
//print_LOG("send request:%s\n", header);
//解析回复
response = get_response();
get_resp_header(response, &resp);
print_LOG("response:%s\n", response);
free(response);
strcpy(resp.file_name, file_name);
print_LOG("\nres:\n content_length:%d\n file_name:%s\n Content-Type:%s\n",
resp.file_name,
//下载⽂件
print_LOG("\ndownload %s start ...\n", resp.file_name);    download_writefile();
return 0;
}
download.h
#ifndef __HTTP_DOWNLOAD_H
#define __HTTP_DOWNLOAD_H
int http_download_file(char *url);
#endif
tcp.c
//-------tcp相关头⽂件------
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> //close()
static int socket_fd = 0;
//tcp_client_init()
//1、创建socket
//2、配置为客户端
//3、配置要连接的服务器ip和端⼝以及协议类型
//4、连接服务器
//5、收发数据
//6、关闭连接
int tcp_client_init(char *ip, int port)
{
int ret;
//1 2
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd == -1)
{
printf("create socket fail\n");
return -1;
}
//3
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;//IPv4协议
servaddr.sin_port = htons(port);//服务器端⼝号
servaddr.sin_addr.s_addr = inet_addr(ip);//设置服务器
//4
ret = connect(socket_fd, &servaddr, sizeof(servaddr)); if(ret == -1)
{
printf("connect %s fail\n", ip);
return -1;
}
}
//tcp_client_send()
int tcp_client_send(char *buff, int len)
{
int ret = 0;
ret = write(socket_fd, buff, len);
return ret;
}
//tcp_client_rcv()
int tcp_client_rcv(char *buff, int len)
{
int ret;
ret = read(socket_fd, buff, len);
return ret;
}
//tcp_client_close()
int tcp_client_close()
{
close(socket_fd);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。