Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Allow to use Netscape cookie file with option -C
Browse files Browse the repository at this point in the history
  • Loading branch information
e2iplayer committed Mar 15, 2019
1 parent 6ed55a2 commit ff62f16
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
45 changes: 42 additions & 3 deletions src/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>

#include <pthread.h>
#include <curl/curl.h>
#include <assert.h>
#include "msg.h"
Expand All @@ -14,6 +15,8 @@ struct http_session {
void *headers;
char *user_agent;
char *proxy_uri;
char *cookie_file;
void *cookie_file_mutex;
long speed_limit;
long speed_time;
};
Expand Down Expand Up @@ -116,6 +119,22 @@ void * set_proxy_uri_http_session(void *ptr_session, const char *proxy_uri)
return session;
}

void * set_cookie_file_session(void *ptr_session, const char *cookie_file, void *cookie_file_mutex)
{
struct http_session *session = ptr_session;
assert(session);

if (cookie_file) {
if (session->cookie_file) {
free(session->cookie_file);
}
session->cookie_file = strdup(cookie_file);
session->cookie_file_mutex = cookie_file_mutex;
}

return session;
}

void add_custom_header_http_session(void *ptr_session, const char *header)
{
struct http_session *session = ptr_session;
Expand Down Expand Up @@ -163,7 +182,8 @@ long get_data_from_url_with_session(void **ptr_session, char *url, char **out, s
curl_easy_setopt(c, CURLOPT_RANGE, range);
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(c, CURLOPT_WRITEDATA, (void *)&chunk);

curl_easy_setopt(c, CURLOPT_VERBOSE, 1L);

if (session->speed_limit) {
curl_easy_setopt(c, CURLOPT_LOW_SPEED_LIMIT, session->speed_limit);
}
Expand All @@ -189,6 +209,17 @@ long get_data_from_url_with_session(void **ptr_session, char *url, char **out, s
if (session->proxy_uri) {
curl_easy_setopt(c, CURLOPT_PROXY, session->proxy_uri);
}

if (session->cookie_file) {
curl_easy_setopt(c, CURLOPT_COOKIEJAR, session->cookie_file);
curl_easy_setopt(c, CURLOPT_COOKIEFILE, session->cookie_file);

if (session->cookie_file_mutex) {
pthread_mutex_lock(session->cookie_file_mutex);
curl_easy_setopt(c, CURLOPT_COOKIELIST, "RELOAD");
pthread_mutex_unlock(session->cookie_file_mutex);
}
}

curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);

Expand Down Expand Up @@ -224,6 +255,12 @@ long get_data_from_url_with_session(void **ptr_session, char *url, char **out, s
free(chunk.memory);
}

if (session->cookie_file && session->cookie_file_mutex) {
pthread_mutex_lock(session->cookie_file_mutex);
curl_easy_setopt(c, CURLOPT_COOKIELIST, "FLUSH");
pthread_mutex_unlock(session->cookie_file_mutex);
}

return http_code;
}

Expand All @@ -232,15 +269,17 @@ void clean_http_session(void *ptr_session)
struct http_session *session = ptr_session;
curl_easy_cleanup(session->handle);

/* free user agent if set */
if (session->user_agent) {
free(session->user_agent);
}

/* free user agent if set */
if (session->proxy_uri) {
free(session->proxy_uri);
}

if (session->cookie_file) {
free(session->cookie_file);
}

/* free the custom headers if set */
if (session->headers) {
Expand Down
1 change: 1 addition & 0 deletions src/curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern "C" {
void * init_http_session(void);
void * set_user_agent_http_session(void *ptr_session, const char *user_agent);
void * set_proxy_uri_http_session(void *ptr_session, const char *proxy_uri);
void * set_cookie_file_session(void *ptr_session, const char *cookie_file, void *cookie_file_mutex);
void * set_timeout_session(void *ptr_session, const long speed_limit, const long speed_time);
void add_custom_header_http_session(void *ptr_session, const char *header);
long get_data_from_url_with_session(void **session, char *url, char **out, size_t *size, int type, char **new_url, const char *range);
Expand Down
11 changes: 11 additions & 0 deletions src/hls.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ static void set_hls_http_header(void *session)
set_proxy_uri_http_session(session, hls_args.proxy_uri);
}

if (hls_args.cookie_file) {
set_cookie_file_session(session, hls_args.cookie_file, hls_args.cookie_file_mutex);
}

for (int i=0; i<HLSDL_MAX_NUM_OF_CUSTOM_HEADERS; ++i) {
if (hls_args.custom_headers[i]) {
add_custom_header_http_session(session, hls_args.custom_headers[i]);
Expand Down Expand Up @@ -1176,12 +1180,14 @@ int download_live_hls(write_ctx_t *out_ctx, hls_media_playlist_t *me)

/* declaration synchronization prymitives */
pthread_mutex_t media_playlist_mtx;
pthread_mutex_t cookie_file_mtx;

pthread_cond_t media_playlist_refresh_cond;
pthread_cond_t media_playlist_empty_cond;

/* init synchronization prymitives */
pthread_mutex_init(&media_playlist_mtx, NULL);
pthread_mutex_init(&cookie_file_mtx, NULL);

pthread_cond_init(&media_playlist_refresh_cond, NULL);
pthread_cond_init(&media_playlist_empty_cond, NULL);
Expand All @@ -1192,6 +1198,8 @@ int download_live_hls(write_ctx_t *out_ctx, hls_media_playlist_t *me)
updater_params.media_playlist_refresh_cond = (void *)&media_playlist_refresh_cond;
updater_params.media_playlist_empty_cond = (void *)&media_playlist_empty_cond;

hls_args.cookie_file_mutex = (void *)&cookie_file_mtx;

// skip first segments
if (me->first_media_segment != me->last_media_segment) {
struct hls_media_segment *ms = me->last_media_segment;
Expand Down Expand Up @@ -1330,6 +1338,9 @@ int download_live_hls(write_ctx_t *out_ctx, hls_media_playlist_t *me)
pthread_cond_destroy(&media_playlist_refresh_cond);
pthread_cond_destroy(&media_playlist_empty_cond);

pthread_mutex_destroy(&cookie_file_mtx);
hls_args.cookie_file_mutex = NULL;

MSG_API("{\"t_d\":%u,\"d_d\":%u,\"d_s\":%"PRId64"}\n", (uint32_t)(me->total_duration_ms / 1000), (uint32_t)(downloaded_duration_ms / 1000), download_size);
if (session)
{
Expand Down
10 changes: 7 additions & 3 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

static void print_help(const char *filename)
{
printf("hlsdl v0.24\n");
printf("hlsdl v0.25\n");
printf("(c) 2017-2019 @selsta, [email protected]\n");
printf("Usage: %s url [options]\n\n"
"-b ... Automaticly choose the best quality.\n"
Expand All @@ -36,7 +36,8 @@ static void print_help(const char *filename)
"-r ... Set max retries at open.\n"
"-w ... Set max download segment retries.\n"
"-a ... Set additional url to the audio media playlist.\n"
"-c ... Treat HTTP code 206 as 200 even if request was made without range header.\n", filename);
"-c ... Treat HTTP code 206 as 200 even if request was made without range header.\n"
"-C ... the file name of file holding cookie data in the old Netscape / Mozilla cookie data format.\n", filename);
exit(0);
}

Expand All @@ -45,7 +46,7 @@ int parse_argv(int argc, char * const argv[])
int ret = 0;
int c = 0;
int custom_header_idx = 0;
while ( (c = getopt(argc, argv, "bvqbfctdo:u:h:s:r:w:e:p:k:n:a:")) != -1)
while ( (c = getopt(argc, argv, "bvqbfctdo:u:h:s:r:w:e:p:k:n:a:C:")) != -1)
{
switch (c)
{
Expand Down Expand Up @@ -91,6 +92,9 @@ int parse_argv(int argc, char * const argv[])
case 'u':
hls_args.user_agent = optarg;
break;
case 'C':
hls_args.cookie_file = optarg;
break;
case 'p':
hls_args.proxy_uri = optarg;
break;
Expand Down
2 changes: 2 additions & 0 deletions src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ struct hls_args {
char *(custom_headers[HLSDL_MAX_NUM_OF_CUSTOM_HEADERS]);
char *key_uri_replace_old;
char *key_uri_replace_new;
char *cookie_file;
void *cookie_file_mutex;
bool accept_partial_content;
};

Expand Down

0 comments on commit ff62f16

Please sign in to comment.