Skip to content

Commit

Permalink
registry: Fix a bug with parsing gitlab registries, fix downloading from
Browse files Browse the repository at this point in the history
public gitlab repositories
  • Loading branch information
nouwaarom committed Apr 10, 2021
1 parent dd7153c commit b099c9e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
27 changes: 18 additions & 9 deletions src/registry/gitlab-registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@
#include "http-get/http-get.h"
#include "registry-internal.h"
#include <curl/curl.h>
#include <string.h>
#include <strdup/strdup.h>
#include <string.h>

static char *string_split(char *in, char sep) {
char *next_sep = strchr(in, sep);
if (next_sep == NULL) {
return next_sep;
}
*next_sep = '\0';
return next_sep + sizeof(char);
}

/**
* Parse a list of packages from the given `html`
Expand All @@ -21,7 +30,7 @@ static list_t *gitlab_registry_parse(const char *hostname, const char *html) {
char *input = strdup(html);
char *line = input;
char *category = NULL;
while ((line = strtok(line, "\n"))) {
while ((line = string_split(line, '\n'))) {
char *dash_position = strstr(line, "-");
// The line starts with a dash, so we expect a package.
if (dash_position != NULL && dash_position - line < 4) {
Expand Down Expand Up @@ -58,14 +67,14 @@ static list_t *gitlab_registry_parse(const char *hostname, const char *html) {
list_t *gitlab_registry_fetch(const char *url, const char *hostname, const char *secret) {
http_get_response_t *res;
if (secret == NULL) {
return NULL;
res = http_get(url, NULL, 0);
} else {
char *key = "PRIVATE-TOKEN";
unsigned int size = strlen(key) + strlen(secret) + 2;
char *authentication_header = malloc(size);
snprintf(authentication_header, size, "%s:%s", key, secret);
res = http_get(url, &authentication_header, 1);
}

char *key = "PRIVATE-TOKEN";
unsigned int size = strlen(key) + strlen(secret) + 2;
char *authentication_header = malloc(size);
snprintf(authentication_header, size, "%s:%s", key, secret);
res = http_get(url, &authentication_header, 1);
if (!res->ok) {
return NULL;
}
Expand Down
9 changes: 6 additions & 3 deletions src/repository/gitlab-repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
#include <stdlib.h>
#include <string.h>
#include <url/url.h>
#include "str-replace/str-replace.h"

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

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

int size = strlen(parsed->hostname) + strlen(parsed->pathname) + strlen(file) + 64;
char* encoded_filename = str_replace(file, "/", "%2F");

size_t size = strlen(parsed->hostname) + strlen(parsed->pathname) + strlen(encoded_filename) + strlen(GITLAB_API_V4_URL) + 1;
char *url = malloc(size);
if (url) {
snprintf(url, size, GITLAB_API_V4_URL, parsed->hostname, parsed->pathname, file);
snprintf(url, size, GITLAB_API_V4_URL, parsed->hostname, parsed->pathname, encoded_filename);
}

url_free(parsed);
Expand Down
4 changes: 2 additions & 2 deletions src/repository/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ http_get_response_t *repository_fetch_package_manifest(const char *package_url,
char *manifest_url = repository_create_url_for_file(package_url, package_id, version, manifest_file, secret);

http_get_response_t *res;
if (strstr(package_url, "gitlab") != NULL) {
if (secret && strstr(package_url, "gitlab") != NULL) {
char *key = "PRIVATE-TOKEN";
unsigned int size = strlen(key) + strlen(secret) + 2;
char *authentication_header = malloc(size);
Expand Down Expand Up @@ -152,7 +152,7 @@ static int fetch_package_file_work(const char *url, const char *dir, const char
pthread_mutex_unlock(&mutex);
#endif

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

0 comments on commit b099c9e

Please sign in to comment.