Skip to content

Commit

Permalink
add experimental mimetype detection support
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tornblom committed May 29, 2024
1 parent 303d1a9 commit 3e36c71
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile.pc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ BIN := websrv.pc
SRCS := src/main.c src/websrv.c src/asset.c src/fs.c
SRCS += src/pc/sys.c

LDADD := -lmicrohttpd
LDADD := -lmicrohttpd -lmagic

ASSETS := $(wildcard assets/*)
GEN_SRCS := $(patsubst assets/%,gen/%, $(ASSETS:=.c))
Expand Down
3 changes: 2 additions & 1 deletion Makefile.ps5
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ BIN := websrv.elf
SRCS := src/main.c src/websrv.c src/asset.c src/fs.c
SRCS += src/ps5/sys.c src/ps5/pt.c src/ps5/elfldr.c src/ps5/hbldr.c
CFLAGS := -Wall -Werror -Isrc
LDADD := -lkernel_sys -lmicrohttpd -lSceSystemService -lSceUserService
LDADD := -lkernel_sys -lSceSystemService -lSceUserService
LDADD += -lmagic -lmicrohttpd

ASSETS := $(wildcard assets/*)
GEN_SRCS := $(patsubst assets/%,gen/%, $(ASSETS:=.c))
Expand Down
37 changes: 37 additions & 0 deletions libmagic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Copyright (C) 2024 John Törnblom
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not see
# <http://www.gnu.org/licenses/>.

LIB_URL="https://astron.com/pub/file/"
LIB_VER="5.45"

if [[ -z "$PS5_PAYLOAD_SDK" ]]; then
echo "error: PS5_PAYLOAD_SDK is not set"
exit 1
fi

source ${PS5_PAYLOAD_SDK}/toolchain/prospero.sh || exit 1

TEMPDIR=$(mktemp -d)
trap 'rm -rf -- "$TEMPDIR"' EXIT

wget -O $TEMPDIR/lib.tar.gz $LIB_URL/file-$LIB_VER.tar.gz || exit 1
tar xf $TEMPDIR/lib.tar.gz -C $TEMPDIR || exit 1

cd $TEMPDIR/file-$LIB_VER
./configure --prefix="${PS5_SYSROOT}" --host=x86_64 \
--disable-shared --enable-static
${MAKE} install DESTDIR="/"
18 changes: 18 additions & 0 deletions src/asset.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ along with this program; see the file COPYING. If not, see
#include <stdlib.h>
#include <string.h>

#include <magic.h>
#include <microhttpd.h>

#include "asset.h"
Expand All @@ -38,6 +39,7 @@ along with this program; see the file COPYING. If not, see

typedef struct asset {
const char *path;
const char *mime;
void *data;
size_t size;
struct asset *next;
Expand All @@ -50,12 +52,23 @@ static asset_t* g_asset_head = 0;
void
asset_register(const char* path, void* data, size_t size) {
asset_t* a = calloc(1, sizeof(asset_t));
const char* mime;
magic_t m;

a->path = path;
a->data = data;
a->size = size;
a->next = g_asset_head;

if((m=magic_open(MAGIC_MIME))) {
if(!magic_load(m, NULL)) {
if((mime=magic_buffer(m, data, size))) {
a->mime = strdup(mime);
}
}
magic_close(m);
}

g_asset_head = a;
}

Expand All @@ -67,18 +80,23 @@ asset_request(struct MHD_Connection *conn, const char* url) {
size_t size = strlen(PAGE_404);
struct MHD_Response *resp;
void* data = PAGE_404;
const char* mime = 0;

for(asset_t* a=g_asset_head; a!=0; a=a->next) {
if(!strcmp(url, a->path)) {
data = a->data;
size = a->size;
mime = a->mime;
status = MHD_HTTP_OK;
break;
}
}

if((resp=MHD_create_response_from_buffer(size, data,
MHD_RESPMEM_PERSISTENT))) {
if(mime) {
MHD_add_response_header(resp, "Content-Type", mime);
}
ret = websrv_queue_response(conn, status, resp);
MHD_destroy_response(resp);
}
Expand Down
12 changes: 12 additions & 0 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ along with this program; see the file COPYING. If not, see
#include <sys/stat.h>
#include <sys/user.h>

#include <magic.h>
#include <microhttpd.h>

#include "fs.h"
Expand Down Expand Up @@ -217,8 +218,10 @@ static enum MHD_Result
file_request(struct MHD_Connection *conn, const char* path) {
enum MHD_Result ret = MHD_NO;
struct MHD_Response *resp;
const char* mime;
struct stat st;
FILE *file = 0;
magic_t m;

if(!stat(path, &st)) {
file = fopen(path, "rb");
Expand All @@ -236,6 +239,15 @@ file_request(struct MHD_Connection *conn, const char* path) {
if((resp=MHD_create_response_from_callback(st.st_size, 32 * PAGE_SIZE,
&file_read, file,
&file_close))) {
if((m=magic_open(MAGIC_MIME))) {
if(!magic_load(m, NULL)) {
if((mime=magic_file(m, path))) {
MHD_add_response_header(resp, "Content-Type", mime);
}
}
magic_close(m);
}

ret = websrv_queue_response (conn, MHD_HTTP_OK, resp);
MHD_destroy_response(resp);
return ret;
Expand Down

0 comments on commit 3e36c71

Please sign in to comment.