Move to mime file

FossilOrigin-Name: 613210e2664cf18ce97174b814ca90655f0e65ab590af88cb202c9f35c90a8da
This commit is contained in:
me@ow.nekobit.net 2022-04-07 03:25:35 +00:00
parent 5f8eaa12b0
commit 9d3607a6eb
3 changed files with 86 additions and 37 deletions

61
src/mime.c Normal file
View file

@ -0,0 +1,61 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <fcgi_stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mime.h"
char* get_mime_boundary()
{
if (!getenv("CONTENT_TYPE")) return 1;
// Data gets changed in place
char* content_type = malloc(strlen(getenv("CONTENT_TYPE"))+1);
if (!content_type)
{
perror("malloc");
exit(1);
}
char* bound_str;
char* boundary;
/* Tmp reading variables */
char* tmp;
if (strstr(content_type, "multipart/form-data") == NULL ||
(bound_str = strstr(content_type, "boundary")) == NULL)
goto error;
bound_str += sizeof("boundary")-1;
boundary = (tmp = strchr(bound_str, '\"')) ? tmp :
strchr(bound_str, '=');
if (!boundary)
goto error;
boundary++;
if ((tmp = strchr(boundary, '\"')))
*tmp = '\0';
return 0;
error:
free(content_type);
return 1;
}

25
src/mime.h Normal file
View file

@ -0,0 +1,25 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MIME_H
#define MIME_H
char* get_mime_boundary();
char* read_mime_data(char* boundary, char* begin, struct http_mime_info* info);
#endif /* MIME_H */

View file

@ -46,37 +46,6 @@ struct status_args
struct mstdnt_status* status;
};
/* A cheap parser for multipart data
* It is designed only for files, don't use this for anything else */
static int parse_files(char*** files, size_t* files_len)
{
char* content_type = getenv("CONTENT_TYPE");
char* bound_str;
char* boundary;
/* Tmp reading variables */
char* tmp;
if (strstr(content_type, "multipart/form-data") == NULL ||
(bound_str = strstr(content_type, "boundary")) == NULL)
return 1;
bound_str += sizeof("boundary")-1;
boundary = (tmp = strchr(bound_str, '\"')) ? tmp :
strchr(bound_str, '=');
if (!boundary)
return 1;
boundary++;
if ((tmp = strchr(boundary, '\"')))
*tmp = '\0';
return 0;
}
int try_post_status(struct session* ssn, mastodont_t* api)
{
if (!(ssn->post.content)) return 1;
@ -86,12 +55,6 @@ int try_post_status(struct session* ssn, mastodont_t* api)
char** files;
size_t files_len;
if (parse_files(&files, &files_len) == 0)
{
}
return 1;
// Cookie copy and read
struct mstdnt_args args = {
.content_type = "text/plain",