http.c: use buffer.h
This commit is contained in:
parent
6456ac58bc
commit
dcd515b4b1
3 changed files with 25 additions and 39 deletions
2
dlight.c
2
dlight.c
|
|
@ -80,7 +80,7 @@ static void process_items(rss_t rss, struct target *t) {
|
||||||
static void process(struct cconf *config) {
|
static void process(struct cconf *config) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
struct http_data *data;
|
struct buffer *data;
|
||||||
|
|
||||||
dlhist_purge(DLHIST_PURGE_INTERVAL);
|
dlhist_purge(DLHIST_PURGE_INTERVAL);
|
||||||
|
|
||||||
|
|
|
||||||
50
http.c
50
http.c
|
|
@ -99,16 +99,9 @@ static size_t hdr_fname_cb(void *src, size_t smemb, size_t nmemb, void *data) {
|
||||||
|
|
||||||
static size_t write_cb(void *src, size_t smemb, size_t nmemb, void *data) {
|
static size_t write_cb(void *src, size_t smemb, size_t nmemb, void *data) {
|
||||||
|
|
||||||
struct http_data *dest = (struct http_data *) data;
|
|
||||||
size_t size = smemb * nmemb;
|
size_t size = smemb * nmemb;
|
||||||
|
|
||||||
dest->block = realloc(dest->block, dest->len + size);
|
buffer_append(data, src, size);
|
||||||
if (dest->block == NULL) {
|
|
||||||
error("out of memory");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
memcpy(dest->block + dest->len, src, size);
|
|
||||||
dest->len += size;
|
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
@ -166,27 +159,26 @@ static int http_request(const char *url, void *req, int mode) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct http_data* http_fetch_page(const char *url) {
|
struct buffer* http_fetch_page(const char *url) {
|
||||||
|
|
||||||
struct http_data *data = malloc(sizeof(struct http_data));
|
struct buffer *buf = malloc(sizeof(struct buffer));
|
||||||
|
|
||||||
data->block = NULL;
|
buffer_init(buf);
|
||||||
data->len = 0;
|
|
||||||
|
|
||||||
if (http_request(url, data, HTTPREQ_MEM) < 0) {
|
if (http_request(url, buf, HTTPREQ_MEM) < 0) {
|
||||||
http_free(data);
|
buffer_free(buf);
|
||||||
|
free(buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return data;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct http_file* http_fetch_file(const char *url) {
|
struct http_file* http_fetch_file(const char *url) {
|
||||||
|
|
||||||
struct http_file *file = malloc(sizeof(struct http_file));
|
struct http_file *file = malloc(sizeof(struct http_file));
|
||||||
|
|
||||||
file->data.block = NULL;
|
|
||||||
file->data.len = 0;
|
|
||||||
file->filename = NULL;
|
file->filename = NULL;
|
||||||
|
buffer_init(&file->data);
|
||||||
|
|
||||||
if (http_request(url, file, HTTPREQ_FILEMEM) < 0) {
|
if (http_request(url, file, HTTPREQ_FILEMEM) < 0) {
|
||||||
http_free_file(file);
|
http_free_file(file);
|
||||||
|
|
@ -240,22 +232,20 @@ error:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_free(struct http_data *data) {
|
void http_free(struct buffer *b) {
|
||||||
|
|
||||||
if (!data)
|
if (b) {
|
||||||
return;
|
buffer_free(b);
|
||||||
if (data->block)
|
free(b);
|
||||||
free(data->block);
|
}
|
||||||
free(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_free_file(struct http_file *file) {
|
void http_free_file(struct http_file *file) {
|
||||||
|
|
||||||
if (!file)
|
if (file) {
|
||||||
return;
|
buffer_free(&file->data);
|
||||||
if (file->data.block)
|
if (file->filename)
|
||||||
free(file->data.block);
|
free(file->filename);
|
||||||
if (file->filename)
|
free(file);
|
||||||
free(file->filename);
|
}
|
||||||
free(file);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
http.h
12
http.h
|
|
@ -21,24 +21,20 @@
|
||||||
#define HTTP_H
|
#define HTTP_H
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include "buffer.h"
|
||||||
struct http_data {
|
|
||||||
void *block;
|
|
||||||
size_t len;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct http_file {
|
struct http_file {
|
||||||
struct http_data data;
|
struct buffer data;
|
||||||
char *filename;
|
char *filename;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct http_data* http_fetch_page(const char *url);
|
struct buffer* http_fetch_page(const char *url);
|
||||||
|
|
||||||
struct http_file* http_fetch_file(const char *url);
|
struct http_file* http_fetch_file(const char *url);
|
||||||
|
|
||||||
int http_download_file(const char *url, const char *dir);
|
int http_download_file(const char *url, const char *dir);
|
||||||
|
|
||||||
void http_free(struct http_data *data);
|
void http_free(struct buffer *b);
|
||||||
|
|
||||||
void http_free_file(struct http_file *file);
|
void http_free_file(struct http_file *file);
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue