Skip to content

Commit

Permalink
use application/octet-stream for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Apr 12, 2024
1 parent 75ad03f commit 75a5564
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions titan.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
typedef struct TitanParams {
char *token;
const char *mime;
size_t size;
struct stat stbuf;
void *body;
} TitanParams;

Expand All @@ -33,10 +33,10 @@ static int titan_request(const URL *url, SSL *ssl, void *p) {
const TitanParams *params = (const TitanParams *)p;
int len, err;

len = snprintf(buffer, sizeof(buffer), params->token == NULL || *params->token == '\0' ? "%s;mime=%s;size=%zu\r\n" : "%s;mime=%s;size=%zu;token=%s\r\n", url->url, params->mime, params->size, params->token);
len = snprintf(buffer, sizeof(buffer), params->token == NULL || *params->token == '\0' ? "%s;mime=%s;size=%zu\r\n" : "%s;mime=%s;size=%zu;token=%s\r\n", url->url, params->mime, params->stbuf.st_size, params->token);
if ((err = SSL_get_error(ssl, SSL_write(ssl, buffer, len >= (int)sizeof(buffer) ? (int)sizeof(buffer) - 1 : len))) != SSL_ERROR_NONE) return err;

return params->size > 0 ? SSL_get_error(ssl, SSL_write(ssl, params->body, params->size)) : SSL_ERROR_NONE;
return params->stbuf.st_size > 0 ? SSL_get_error(ssl, SSL_write(ssl, params->body, params->stbuf.st_size)) : SSL_ERROR_NONE;
}


Expand All @@ -47,8 +47,7 @@ static void *titan_upload(const Selector *sel, URL *url, char **mime, Parser *pa
char *tmp;
#define magic_close(x) do {} while (0)
#endif
struct stat stbuf;
TitanParams params;
TitanParams params = {.mime = "application/octet-stream"};
char *path;
SSL *ssl = NULL;
int fd, status = -1;
Expand All @@ -57,35 +56,33 @@ static void *titan_upload(const Selector *sel, URL *url, char **mime, Parser *pa

if (!ask) return NULL;

#ifdef GPLACES_USE_LIBMAGIC
if ((mag = magic_open(MAGIC_MIME_TYPE | MAGIC_NO_CHECK_COMPRESS | MAGIC_ERROR)) == NULL) return NULL;
if (magic_load(mag, NULL) != 0) { magic_close(mag); return NULL; }
#endif

if ((params.token = bestline("Token> ")) == NULL) return NULL;
if (interactive) bestlineHistoryAdd(params.token);
if ((path = bestline("File> ")) == NULL) { free(params.token); return NULL; }
if (interactive) bestlineHistoryAdd(path);

if ((fd = open(path, O_RDONLY)) == -1) { error(0, "cannot open `%s`: %s", path, strerror(errno)); free(path); free(params.token); magic_close(mag); return NULL; }
if (fstat(fd, &stbuf) == -1) { error(0, "cannot open `%s`: %s", path, strerror(errno)); close(fd); free(path); free(params.token); magic_close(mag); return NULL; }
if (stbuf.st_size > 0 && (params.body = mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) { error(0, "cannot open `%s`: %s", path, strerror(errno)); close(fd); free(path); free(params.token); magic_close(mag); return NULL; }
if ((fd = open(path, O_RDONLY)) == -1) { error(0, "cannot open `%s`: %s", path, strerror(errno)); free(path); free(params.token); return NULL; }
if (fstat(fd, &params.stbuf) == -1) { error(0, "cannot open `%s`: %s", path, strerror(errno)); close(fd); free(path); free(params.token); return NULL; }
if (params.stbuf.st_size > 0 && (params.body = mmap(NULL, params.stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) { error(0, "cannot open `%s`: %s", path, strerror(errno)); close(fd); free(path); free(params.token); return NULL; }

if (params.stbuf.st_size > 0) {
#ifdef GPLACES_USE_LIBMAGIC
if ((params.mime = magic_buffer(mag, params.body, stbuf.st_size)) == NULL) { error(0, "cannot open `%s`: %s", path, magic_error(mag)); if (stbuf.st_size > 0) munmap(params.body, stbuf.st_size); close(fd); free(path); free(params.token); magic_close(mag); return NULL; }
if ((mag = magic_open(MAGIC_MIME_TYPE | MAGIC_NO_CHECK_COMPRESS | MAGIC_ERROR)) == NULL) { munmap(params.body, params.stbuf.st_size); close(fd); free(path); free(params.token); return NULL; }
if (magic_load(mag, NULL) != 0) { munmap(params.body, params.stbuf.st_size); close(fd); free(path); free(params.token); magic_close(mag); return NULL; }
if ((params.mime = magic_buffer(mag, params.body, params.stbuf.st_size)) == NULL) { error(0, "cannot open `%s`: %s", path, magic_error(mag)); munmap(params.body, params.stbuf.st_size); close(fd); free(path); free(params.token); magic_close(mag); return NULL; }
#else
if ((tmp = bestline("File type> ")) == NULL) { if (stbuf.st_size > 0) munmap(params.body, stbuf.st_size); close(fd); free(path); free(params.token); return NULL; }
if (interactive) bestlineHistoryAdd(tmp);
params.mime = tmp;
if ((tmp = bestline("File type> ")) == NULL) { munmap(params.body, params.stbuf.st_size); close(fd); free(path); free(params.token); return NULL; }
if (interactive) bestlineHistoryAdd(tmp);
params.mime = tmp;
#endif
}

params.size = stbuf.st_size;
do {
status = ssl_download(url, &ssl, mime, titan_request, &params, ask);
if (status >= 20 && status <= 29) break;
} while ((status >= 10 && status <= 19) || (status >= 60 && status <= 69) || (status >= 30 && status <= 39 && ++redirs < 5 && url->proto->download == titan_upload));

if (stbuf.st_size > 0) munmap(params.body, stbuf.st_size);
if (params.stbuf.st_size > 0) munmap(params.body, params.stbuf.st_size);
close(fd);
free(params.token);
#ifdef GPLACES_USE_LIBMAGIC
Expand Down

0 comments on commit 75a5564

Please sign in to comment.