Skip to content

Commit b099c9e

Browse files
committed
registry: Fix a bug with parsing gitlab registries, fix downloading from
public gitlab repositories
1 parent dd7153c commit b099c9e

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

Diff for: src/registry/gitlab-registry.c

+18-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
#include "http-get/http-get.h"
99
#include "registry-internal.h"
1010
#include <curl/curl.h>
11-
#include <string.h>
1211
#include <strdup/strdup.h>
12+
#include <string.h>
13+
14+
static char *string_split(char *in, char sep) {
15+
char *next_sep = strchr(in, sep);
16+
if (next_sep == NULL) {
17+
return next_sep;
18+
}
19+
*next_sep = '\0';
20+
return next_sep + sizeof(char);
21+
}
1322

1423
/**
1524
* Parse a list of packages from the given `html`
@@ -21,7 +30,7 @@ static list_t *gitlab_registry_parse(const char *hostname, const char *html) {
2130
char *input = strdup(html);
2231
char *line = input;
2332
char *category = NULL;
24-
while ((line = strtok(line, "\n"))) {
33+
while ((line = string_split(line, '\n'))) {
2534
char *dash_position = strstr(line, "-");
2635
// The line starts with a dash, so we expect a package.
2736
if (dash_position != NULL && dash_position - line < 4) {
@@ -58,14 +67,14 @@ static list_t *gitlab_registry_parse(const char *hostname, const char *html) {
5867
list_t *gitlab_registry_fetch(const char *url, const char *hostname, const char *secret) {
5968
http_get_response_t *res;
6069
if (secret == NULL) {
61-
return NULL;
70+
res = http_get(url, NULL, 0);
71+
} else {
72+
char *key = "PRIVATE-TOKEN";
73+
unsigned int size = strlen(key) + strlen(secret) + 2;
74+
char *authentication_header = malloc(size);
75+
snprintf(authentication_header, size, "%s:%s", key, secret);
76+
res = http_get(url, &authentication_header, 1);
6277
}
63-
64-
char *key = "PRIVATE-TOKEN";
65-
unsigned int size = strlen(key) + strlen(secret) + 2;
66-
char *authentication_header = malloc(size);
67-
snprintf(authentication_header, size, "%s:%s", key, secret);
68-
res = http_get(url, &authentication_header, 1);
6978
if (!res->ok) {
7079
return NULL;
7180
}

Diff for: src/repository/gitlab-repository.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
#include <stdlib.h>
1010
#include <string.h>
1111
#include <url/url.h>
12+
#include "str-replace/str-replace.h"
1213

1314
#define GITLAB_API_V4_URL "https://%s/api/v4%s/repository/files/%s/raw?ref=master"
1415

1516
// GET :hostname/api/v4/projects/:id/repository/files/:file_path/raw
16-
char* gitlab_repository_get_url_for_file(const char*package_url, const char* slug, const char* version, const char *file, const char* secret) {
17+
char *gitlab_repository_get_url_for_file(const char *package_url, const char *slug, const char *version, const char *file, const char *secret) {
1718
url_data_t *parsed = url_parse(package_url);
1819

19-
int size = strlen(parsed->hostname) + strlen(parsed->pathname) + strlen(file) + 64;
20+
char* encoded_filename = str_replace(file, "/", "%2F");
21+
22+
size_t size = strlen(parsed->hostname) + strlen(parsed->pathname) + strlen(encoded_filename) + strlen(GITLAB_API_V4_URL) + 1;
2023
char *url = malloc(size);
2124
if (url) {
22-
snprintf(url, size, GITLAB_API_V4_URL, parsed->hostname, parsed->pathname, file);
25+
snprintf(url, size, GITLAB_API_V4_URL, parsed->hostname, parsed->pathname, encoded_filename);
2326
}
2427

2528
url_free(parsed);

Diff for: src/repository/repository.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ http_get_response_t *repository_fetch_package_manifest(const char *package_url,
8787
char *manifest_url = repository_create_url_for_file(package_url, package_id, version, manifest_file, secret);
8888

8989
http_get_response_t *res;
90-
if (strstr(package_url, "gitlab") != NULL) {
90+
if (secret && strstr(package_url, "gitlab") != NULL) {
9191
char *key = "PRIVATE-TOKEN";
9292
unsigned int size = strlen(key) + strlen(secret) + 2;
9393
char *authentication_header = malloc(size);
@@ -152,7 +152,7 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
152152
pthread_mutex_unlock(&mutex);
153153
#endif
154154

155-
if (strstr(url, "gitlab") != NULL) {
155+
if (secret && strstr(url, "gitlab") != NULL) {
156156
char *key = "PRIVATE-TOKEN";
157157
unsigned int size = strlen(key) + strlen(secret) + 2;
158158
char *authentication_header = malloc(size);

0 commit comments

Comments
 (0)