Skip to content

Commit 057ed6f

Browse files
committed
Initial commit
1 parent a56e453 commit 057ed6f

14 files changed

+182
-41
lines changed

src/clioptions.c

+25
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,28 @@ int clioptions_parse(
246246
}
247247

248248
strcpy(options->url, arg->value);
249+
} else if (strcmp(arg->key, "base-url") == 0) {
250+
if (options->base_url != NULL) {
251+
err = M3U8ERR_CLI_DUPLICATE_ARGUMENT;
252+
goto end;
253+
}
254+
255+
if (arg->value == NULL) {
256+
err = M3U8ERR_CLI_ARGUMENT_VALUE_MISSING;
257+
goto end;
258+
}
259+
260+
free(options->base_url);
261+
options->base_url = NULL;
262+
263+
options->base_url = malloc(strlen(arg->value) + 1);
264+
265+
if (options->base_url == NULL) {
266+
err = M3U8ERR_MEMORY_ALLOCATE_FAILURE;
267+
goto end;
268+
}
269+
270+
strcpy(options->base_url, arg->value);
249271
} else if (strcmp(arg->key, "key") == 0) {
250272
if (options->key != NULL) {
251273
err = M3U8ERR_CLI_DUPLICATE_ARGUMENT;
@@ -852,6 +874,9 @@ void clioptions_free(struct CLIOptions* const options) {
852874
free(options->url);
853875
options->url = NULL;
854876

877+
free(options->base_url);
878+
options->base_url = NULL;
879+
855880
free(options->key);
856881
options->key = NULL;
857882

src/clioptions.h

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct CLIOptions {
5454
int concurrency;
5555
int retry;
5656
char* url;
57+
char* base_url;
5758
char* key;
5859
char* output;
5960
struct curl_slist* headers;

src/guess_uri.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ int uri_guess_type(const char* const something) {
2121
(a == '.' && (b == '/' || b == '\\')) || (isalpha(a) && b == ':') || (a == '/' || a == '\\')
2222
);
2323

24-
if (!status) {
25-
status = file_exists(something);
26-
}
27-
2824
if (status) {
29-
return GUESS_URI_TYPE_LOCAL_FILE;
25+
if (file_exists(something)) {
26+
return GUESS_URI_TYPE_LOCAL_FILE;
27+
}
28+
29+
if (directory_exists(something)) {
30+
return GUESS_URI_TYPE_LOCAL_DIRECTORY;
31+
}
3032
}
3133

3234
return GUESS_URI_TYPE_SOMETHING_ELSE;

src/guess_uri.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
#define GUESS_URI_TYPE_URL 0x01
55
#define GUESS_URI_TYPE_LOCAL_FILE 0x02
6-
#define GUESS_URI_TYPE_SOMETHING_ELSE 0x03
6+
#define GUESS_URI_TYPE_LOCAL_DIRECTORY 0x03
7+
#define GUESS_URI_TYPE_SOMETHING_ELSE 0x04
78

89
int uri_guess_type(const char* const something);
910

src/m3u8.c

+69-17
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,9 @@ void m3u8playlist_free(struct M3U8Playlist* const playlist) {
27632763
free(playlist->uri.uri);
27642764
playlist->uri.uri = NULL;
27652765

2766+
free(playlist->suburi.uri);
2767+
playlist->suburi.uri = NULL;
2768+
27662769
if (!playlist->subresource) {
27672770
m3u8httpclient_free(&playlist->client);
27682771
m3u8mhttpclient_free(&playlist->multi_client);
@@ -3604,7 +3607,8 @@ int m3u8_dump_file(
36043607
static int m3u8playlist_seturi(
36053608
struct M3U8Playlist* const playlist,
36063609
const enum M3U8BaseURIType type,
3607-
const char* const uri
3610+
const char* const uri,
3611+
const char* const suburi
36083612
) {
36093613
/*
36103614
Set the base URI of the M3U8 playlist.
@@ -3613,17 +3617,53 @@ static int m3u8playlist_seturi(
36133617
or an HTTP URL.
36143618
*/
36153619

3620+
int err = M3U8ERR_SUCCESS;
3621+
int subtype = 0;
3622+
36163623
playlist->uri.type = type;
36173624

36183625
playlist->uri.uri = malloc(strlen(uri) + 1);
36193626

36203627
if (playlist->uri.uri == NULL) {
3621-
return M3U8ERR_MEMORY_ALLOCATE_FAILURE;
3628+
err = M3U8ERR_MEMORY_ALLOCATE_FAILURE;
3629+
goto end;
36223630
}
36233631

36243632
strcpy(playlist->uri.uri, uri);
36253633

3626-
return M3U8ERR_SUCCESS;
3634+
if (suburi == NULL) {
3635+
goto end;
3636+
}
3637+
3638+
subtype = uri_guess_type(suburi);
3639+
3640+
switch (subtype) {
3641+
case GUESS_URI_TYPE_URL:
3642+
playlist->suburi.type = M3U8_BASE_URI_TYPE_URL;
3643+
break;
3644+
case GUESS_URI_TYPE_LOCAL_FILE:
3645+
playlist->suburi.type = M3U8_BASE_URI_TYPE_LOCAL_FILE;
3646+
break;
3647+
case GUESS_URI_TYPE_LOCAL_DIRECTORY:
3648+
playlist->suburi.type = M3U8_BASE_URI_TYPE_LOCAL_DIRECTORY;
3649+
break;
3650+
default:
3651+
err = M3U8ERR_LOAD_UNSUPPORTED_URI;
3652+
goto end;
3653+
}
3654+
3655+
playlist->suburi.uri = malloc(strlen(suburi) + 1);
3656+
3657+
if (playlist->suburi.uri == NULL) {
3658+
err = M3U8ERR_MEMORY_ALLOCATE_FAILURE;
3659+
goto end;
3660+
}
3661+
3662+
strcpy(playlist->suburi.uri, suburi);
3663+
3664+
end:;
3665+
3666+
return err;
36273667

36283668
}
36293669

@@ -3639,7 +3679,8 @@ int m3u8playlist_load_buffer(
36393679

36403680
int m3u8playlist_load_file(
36413681
struct M3U8Playlist* const playlist,
3642-
const char* const filename
3682+
const char* const filename,
3683+
const char* const base_uri
36433684
) {
36443685

36453686
int status = 0;
@@ -3718,7 +3759,8 @@ int m3u8playlist_load_file(
37183759
err = m3u8playlist_seturi(
37193760
playlist,
37203761
M3U8_BASE_URI_TYPE_LOCAL_FILE,
3721-
absolute_path
3762+
absolute_path,
3763+
base_uri
37223764
);
37233765

37243766
if (err != M3U8ERR_SUCCESS) {
@@ -3756,7 +3798,8 @@ size_t curl_write_string_cb(char* ptr, size_t size, size_t nmemb, void* userdata
37563798

37573799
int m3u8playlist_load_url(
37583800
struct M3U8Playlist* const playlist,
3759-
const char* const url
3801+
const char* const url,
3802+
const char* const base_uri
37603803
) {
37613804

37623805
int err = 0;
@@ -3827,7 +3870,8 @@ int m3u8playlist_load_url(
38273870
err = m3u8playlist_seturi(
38283871
playlist,
38293872
M3U8_BASE_URI_TYPE_URL,
3830-
effective_url
3873+
effective_url,
3874+
base_uri
38313875
);
38323876

38333877
if (err != M3U8ERR_SUCCESS) {
@@ -3865,7 +3909,8 @@ int m3u8playlist_load_url(
38653909

38663910
int m3u8playlist_load(
38673911
struct M3U8Playlist* const playlist,
3868-
const char* const something
3912+
const char* const something,
3913+
const char* const base_uri
38693914
) {
38703915

38713916
int err = 0;
@@ -3874,10 +3919,10 @@ int m3u8playlist_load(
38743919

38753920
switch (type) {
38763921
case GUESS_URI_TYPE_URL:
3877-
err = m3u8playlist_load_url(playlist, something);
3922+
err = m3u8playlist_load_url(playlist, something, base_uri);
38783923
break;
38793924
case GUESS_URI_TYPE_LOCAL_FILE:
3880-
err = m3u8playlist_load_file(playlist, something);
3925+
err = m3u8playlist_load_file(playlist, something, base_uri);
38813926
break;
38823927
case GUESS_URI_TYPE_SOMETHING_ELSE:
38833928
err = m3u8playlist_load_buffer(playlist, something);
@@ -3899,21 +3944,23 @@ int m3u8playlist_load_subresource(
38993944

39003945
int err = 0;
39013946

3947+
const struct M3U8BaseURI* const base_uri = (root->suburi.uri == NULL) ? &root->uri : &root->suburi;
3948+
39023949
char* uri = NULL;
39033950

39043951
subresource->subresource = 1;
39053952

3906-
switch (root->uri.type) {
3953+
switch (base_uri->type) {
39073954
case M3U8_BASE_URI_TYPE_URL: {
39083955
CURLcode code = CURLE_OK;
39093956

3910-
err = m3u8uri_resolve_url(root->uri.uri, something, &uri);
3957+
err = m3u8uri_resolve_url(base_uri->uri, something, &uri);
39113958

39123959
if (err != M3U8ERR_SUCCESS) {
39133960
goto end;
39143961
}
39153962

3916-
code = curl_easy_setopt(root->client.curl, CURLOPT_REFERER, root->uri.uri);
3963+
code = curl_easy_setopt(root->client.curl, CURLOPT_REFERER, base_uri->uri);
39173964

39183965
if (code != CURLE_OK) {
39193966
err = M3U8ERR_CURL_SETOPT_FAILURE;
@@ -3922,7 +3969,7 @@ int m3u8playlist_load_subresource(
39223969

39233970
memcpy(&subresource->client, &root->client, sizeof(root->client));
39243971

3925-
err = m3u8playlist_load_url(subresource, uri);
3972+
err = m3u8playlist_load_url(subresource, uri, NULL);
39263973

39273974
code = curl_easy_setopt(root->client.curl, CURLOPT_REFERER, NULL);
39283975

@@ -3933,14 +3980,19 @@ int m3u8playlist_load_subresource(
39333980

39343981
break;
39353982
}
3983+
case M3U8_BASE_URI_TYPE_LOCAL_DIRECTORY:
39363984
case M3U8_BASE_URI_TYPE_LOCAL_FILE: {
3937-
err = m3u8uri_resolve_path(root->uri.uri, something, &uri);
3985+
if (base_uri->type == M3U8_BASE_URI_TYPE_LOCAL_DIRECTORY) {
3986+
err = m3u8uri_resolve_directory(base_uri->uri, something, &uri);
3987+
} else {
3988+
err = m3u8uri_resolve_file(base_uri->uri, something, &uri);
3989+
}
39383990

39393991
if (err != M3U8ERR_SUCCESS) {
39403992
goto end;
39413993
}
39423994

3943-
err = m3u8playlist_load_file(subresource, uri);
3995+
err = m3u8playlist_load_file(subresource, uri, NULL);
39443996
break;
39453997
}
39463998
case M3U8_BASE_URI_TYPE_TEXT:
@@ -3953,7 +4005,7 @@ int m3u8playlist_load_subresource(
39534005

39544006
end:;
39554007

3956-
if (root->uri.type == M3U8_BASE_URI_TYPE_URL) {
4008+
if (base_uri->type == M3U8_BASE_URI_TYPE_URL) {
39574009
curl_free(uri);
39584010
} else {
39594011
free(uri);

src/m3u8.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,20 @@ int m3u8playlist_load_buffer(
8787

8888
int m3u8playlist_load_file(
8989
struct M3U8Playlist* const playlist,
90-
const char* const filename
90+
const char* const filename,
91+
const char* const base_uri
9192
);
9293

9394
int m3u8playlist_load_url(
9495
struct M3U8Playlist* const playlist,
95-
const char* const url
96+
const char* const url,
97+
const char* const base_uri
9698
);
9799

98100
int m3u8playlist_load(
99101
struct M3U8Playlist* const playlist,
100-
const char* const something
102+
const char* const something,
103+
const char* const base_uri
101104
);
102105

103106
int m3u8playlist_load_subresource(

src/m3u8stream.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -2426,12 +2426,13 @@ int m3u8stream_load_buffer(
24262426

24272427
int m3u8stream_load_file(
24282428
struct M3U8Stream* const stream,
2429-
const char* const filename
2429+
const char* const filename,
2430+
const char* const base_uri
24302431
) {
24312432

24322433
int err = 0;
24332434

2434-
err = m3u8playlist_load_file(&stream->playlist, filename);
2435+
err = m3u8playlist_load_file(&stream->playlist, filename, base_uri);
24352436

24362437
if (err != M3U8ERR_SUCCESS) {
24372438
return err;
@@ -2445,12 +2446,13 @@ int m3u8stream_load_file(
24452446

24462447
int m3u8stream_load_url(
24472448
struct M3U8Stream* const stream,
2448-
const char* const url
2449+
const char* const url,
2450+
const char* const base_uri
24492451
) {
24502452

24512453
int err = 0;
24522454

2453-
err = m3u8playlist_load_url(&stream->playlist, url);
2455+
err = m3u8playlist_load_url(&stream->playlist, url, base_uri);
24542456

24552457
if (err != M3U8ERR_SUCCESS) {
24562458
return err;
@@ -2464,12 +2466,13 @@ int m3u8stream_load_url(
24642466

24652467
int m3u8stream_load(
24662468
struct M3U8Stream* const stream,
2467-
const char* const something
2469+
const char* const something,
2470+
const char* const base_uri
24682471
) {
24692472

24702473
int err = 0;
24712474

2472-
err = m3u8playlist_load(&stream->playlist, something);
2475+
err = m3u8playlist_load(&stream->playlist, something, base_uri);
24732476

24742477
if (err != M3U8ERR_SUCCESS) {
24752478
return err;

src/m3u8stream.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,20 @@ int m3u8stream_load_buffer(
289289

290290
int m3u8stream_load_file(
291291
struct M3U8Stream* const stream,
292-
const char* const filename
292+
const char* const filename,
293+
const char* const base_uri
293294
);
294295

295296
int m3u8stream_load_url(
296297
struct M3U8Stream* const stream,
297-
const char* const url
298+
const char* const url,
299+
const char* const base_uri
298300
);
299301

300302
int m3u8stream_load(
301303
struct M3U8Stream* const stream,
302-
const char* const something
304+
const char* const something,
305+
const char* const base_uri
303306
);
304307

305308
int m3u8stream_load_subresource(

src/m3u8types.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,8 @@ struct M3U8Tags {
256256
enum M3U8BaseURIType {
257257
M3U8_BASE_URI_TYPE_TEXT,
258258
M3U8_BASE_URI_TYPE_URL,
259-
M3U8_BASE_URI_TYPE_LOCAL_FILE
259+
M3U8_BASE_URI_TYPE_LOCAL_FILE,
260+
M3U8_BASE_URI_TYPE_LOCAL_DIRECTORY
260261
};
261262

262263
struct M3U8BaseURI {
@@ -268,6 +269,7 @@ struct M3U8Playlist {
268269
enum M3U8PlaylistType type;
269270
struct M3U8Tags tags;
270271
struct M3U8BaseURI uri;
272+
struct M3U8BaseURI suburi;
271273
struct M3U8HTTPClient client;
272274
struct M3U8MultiHTTPClient multi_client;
273275
int subresource;

0 commit comments

Comments
 (0)