summaryrefslogtreecommitdiff
path: root/frontends/cocoa/fetch.m
blob: 764f25438bea6b69486ba741ea162c37300919c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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.  If not, see <http://www.gnu.org/licenses/>.
 */

#import <Cocoa/Cocoa.h>

#import "utils/log.h"
#import "utils/nsurl.h"
#import "netsurf/fetch.h"

#import "cocoa/fetch.h"

static char cocoafiletype[200];

static const char *fetch_filetype(const char *unix_path)
{
    NSString *path = @(unix_path);
    NSString *uti = [[NSWorkspace sharedWorkspace] typeOfFile:path error:NULL];
    if (uti == nil) {
        // If the file was not found look up the UTI from the file extension.
        uti = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)path.pathExtension, nil);
    }

    if (uti == nil) {
        // We can’t determine the UTI, let’s pretend this is HTML.
        return "text/html";
    }

    NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)uti, kUTTagClassMIMEType);

    strncpy(cocoafiletype, [mimeType cStringUsingEncoding: NSUTF8StringEncoding], sizeof cocoafiletype);

    NSLOG(netsurf, INFO, "\tMIME type for '%s' is '%s'", unix_path, cocoafiletype);

    return cocoafiletype;
}

static NSString *findResource(const char *path) {
    NSString *pathString = @(path);
    NSString *nspath = [[NSBundle mainBundle] pathForResource:pathString ofType:@""];

    if (nspath == nil) {
        nspath = [[NSBundle mainBundle] pathForResource: pathString ofType:@"" inDirectory:nil forLocalization:@"en"];
    }

    return nspath;
}

static nsurl *gui_get_resource_url(const char *path)
{
    NSString *nspath = findResource(path);
    if (nspath == nil) {
        return NULL;
    }

    nsurl *url = NULL;
    nsurl_create([[[NSURL fileURLWithPath:nspath] absoluteString] UTF8String], &url);
    return url;
}

static nserror cocoa_get_resource_data(const char *path, const uint8_t **data, size_t *data_len)
{
    NSString *resourcePath = findResource(path);
    if (resourcePath == nil) {
        return NSERROR_NOT_FOUND;
    }

    int file = open(resourcePath.fileSystemRepresentation, O_RDONLY);
    if (file < 0) {
        return NSERROR_NOT_FOUND;
    }

    int size = lseek(file, 0, SEEK_END);
    if (size < 0) {
        close(file);
        return NSERROR_INVALID;
    }

    void *buffer = malloc(size);
    if (buffer == NULL) {
        close(file);
        return NSERROR_NOMEM;
    }

    lseek(file, 0, SEEK_SET);
    int readAmount = read(file, buffer, size);

    if (size != readAmount) {
        free(buffer);
        close(file);
        return NSERROR_INVALID;
    }

    *data = buffer;
    *data_len = size;

    close(file);

    return NSERROR_OK;
}

static nserror cocoa_release_resource_data(const uint8_t *data)
{
    free((void *)data);
    return NSERROR_OK;
}

static struct gui_fetch_table fetch_table = {
    .filetype = fetch_filetype,

    .get_resource_url = gui_get_resource_url,

    .get_resource_data = cocoa_get_resource_data,
    .release_resource_data = cocoa_release_resource_data,
};

struct gui_fetch_table *cocoa_fetch_table = &fetch_table;