Skip to content

Commit

Permalink
mimetype/extension updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ksherlock committed Aug 11, 2013
1 parent ed6c2fc commit fff9954
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 106 deletions.
62 changes: 43 additions & 19 deletions ftype.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,45 @@

#include <Types.h>

int parse_ftype(const char *cp, Word size, Word *ftype, Word *atype)
int parse_extension(const char *cp, Word *ftype, LongWord *atype)
{
Word size;
Word *wp = (Word *)cp;
Word h;
int i;
int pd;

*ftype = 0;
*atype = 0;

if (!cp || !size) return 0;

if (!cp || !*cp) return 0;

pd = -1;
for (i = 0; ; ++i)
{
char c;

c = cp[i];
if (c == 0) break;
if (c == '.') pd = i;
}

// pd == position of final .
// i == strlen

if (pd == -1) return 0;
if (pd + 1 >= i) return 0;
pd++; // skip past it...

cp += pd;
size = i - pd;

h = ((*cp | 0x20) ^ size) & 0x0f;

switch (h)
{
case 0x00:
// shk
if (size == 3
&& (wp[0] | 0x2020) == 0x6873 // 'sh'
&& (cp[2] | 0x20) == 0x6b // 'k'
) {
*ftype = 0xe0;
*atype = 0x8002;
return 1;
}
// text
if (size == 4
&& (wp[0] | 0x2020) == 0x6574 // 'te'
Expand All @@ -36,6 +51,15 @@ int parse_ftype(const char *cp, Word size, Word *ftype, Word *atype)
*atype = 0x0000;
return 1;
}
// shk
if (size == 3
&& (wp[0] | 0x2020) == 0x6873 // 'sh'
&& (cp[2] | 0x20) == 0x6b // 'k'
) {
*ftype = 0xe0;
*atype = 0x8002;
return 1;
}
break;

case 0x01:
Expand All @@ -51,14 +75,6 @@ int parse_ftype(const char *cp, Word size, Word *ftype, Word *atype)
break;

case 0x02:
// c
if (size == 1
&& (cp[0] | 0x20) == 0x63 // 'c'
) {
*ftype = 0xb0;
*atype = 0x0008;
return 1;
}
// asm
if (size == 3
&& (wp[0] | 0x2020) == 0x7361 // 'as'
Expand All @@ -68,6 +84,14 @@ int parse_ftype(const char *cp, Word size, Word *ftype, Word *atype)
*atype = 0x0003;
return 1;
}
// c
if (size == 1
&& (cp[0] | 0x20) == 0x63 // 'c'
) {
*ftype = 0xb0;
*atype = 0x0008;
return 1;
}
break;

case 0x03:
Expand Down
39 changes: 34 additions & 5 deletions ftype.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,44 @@

#include <Types.h>

int parse_ftype(const char *cp, Word size, Word *ftype, Word *atype)
// cp should be a filename w/ .ext
int parse_extension_c(const char *cp, Word *ftype, LongWord *atype)
{
int i;
int pd;

if (!cp || !*cp) return 0;

pd = -1;
for (i = 0; ; ++i)
{
char c;

c = cp[i];
if (c == 0) break;
if (c == '.') pd = i;
}

// pd == position of final .
// i == strlen

if (pd == -1) return 0;
if (pd + 1 >= i) return 0;
pd++; // skip past it...

return parse_extension(cp + pd, i - pd, ftype, atype);
}

// cp is just the extension
int parse_extension(const char *cp, Word size, Word *ftype, LongWord *atype)
{
Word size;
Word *wp = (Word *)cp;
Word h;

*ftype = 0;
*atype = 0;



if (!cp || !size) return 0;


h = ((*cp | 0x20) ^ size) & 0x0f;

Expand Down
9 changes: 9 additions & 0 deletions gno.orca.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __orca__
#define __orca__

#include <stdio.h>

#define fsetbinary(f) (f->_flag &= ~_IOTEXT)


#endif
13 changes: 10 additions & 3 deletions gopher.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#include "s16debug.h"


extern int setfiletype(const char *filename);
static FileInfoRecGS FileInfo;
static Word FileAttr;

static int gopher_binary(Word ipid, FILE *file)
{
Expand Down Expand Up @@ -282,6 +282,9 @@ int do_gopher(const char *url, URLComponents *components)

file = stdout;


FileAttr = 0;
memset(&FileInfo, 0, sizeof(FileInfo));

if (!components->portNumber) components->portNumber = 70;

Expand Down Expand Up @@ -351,7 +354,11 @@ int do_gopher(const char *url, URLComponents *components)
return -1;
}

setfiletype(filename);
if (parse_extension(filename, &FileInfo.fileType, &FileInfo.auxType))
{
FileAttr |= ATTR_FILETYPE | ATTR_AUXTYPE;
setfileattr(filename, &FileInfo, FileAttr);
}
}


Expand Down
87 changes: 78 additions & 9 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include "http.utils.h"
#include "s16debug.h"


static FileInfoRecGS FileInfo;
static Word FileAttr;

static int do_http_0_9(
const char *url,
URLComponents *components,
Expand Down Expand Up @@ -312,9 +316,7 @@ int read_response(Word ipid, FILE *file, Handle dict)

LongWord contentSize;

int haveTime = 0;
timeGSRec time;

int haveTime = 0;

contentSize = 0;
transferEncoding = -1;
Expand All @@ -326,6 +328,11 @@ int read_response(Word ipid, FILE *file, Handle dict)
transferEncoding = 0;
}

/*
* check the transfer encoding header
* should be chunked or identity (default)
*
*/
value = DictionaryGet(dict, "Transfer-Encoding", 17, &valueSize);
if (value)
{
Expand Down Expand Up @@ -359,6 +366,45 @@ int read_response(Word ipid, FILE *file, Handle dict)
}
}

/*
* convert a content-type header mime string into
* a file type / aux type.
*
*/
value = DictionaryGet(dict, "Content-Type", 12, &valueSize)
if (value && valueSize)
{
int i;
int slash = -1;
// strip ';'
for (i = 0; i < valueSize; ++i)
{
char c = value[i];
if (c == ';') break;
if (c == '/') slash = i;
}

// todo -- flag for this or not.
valueSize = i;
if (parse_mime(value, valueSize,
&FileInfo.fileType,
&FileInfo.auxType))
{
FileAttr |= ATTR_FILETYPE | ATTR_AUXTYPE;
}
else if (slash != -1 && parse_mime(value, slash,
&FileInfo.fileType,
&FileInfo.auxType))
{
FileAttr |= ATTR_FILETYPE | ATTR_AUXTYPE;
}

}

/*
* convert the Last Modified header into a file mod date
*
*/
value = DictionaryGet(dict, "Last-Modified", 13, &valueSize);
if (value && valueSize <= 255)
{
Expand All @@ -367,17 +413,31 @@ int read_response(Word ipid, FILE *file, Handle dict)
pstring = (char *)malloc(valueSize + 1);
if (pstring)
{
struct {
LongWord lo;
LongWord hi;
} comp;

*pstring = valueSize;
memcpy(pstring + 1, value, valueSize);

// parse the last-modified timestamp.
// 0x0e00 is rfc 822 format.
// (which is now obsoleted by rfc 2822 but close enough)
tiParseDateString(&time, pstring, 0x0e00);
if (!_toolErr)


// should use _tiDateString2Sec to get seconds
// then use ConvSeconds to get the date
// this should handle timezones.

tiDateString2Sec(&comp, pstring, 0x0e00);
if (!_toolErr && hi == 0)
{
haveTime = 1;
ConvSeconds(secs2TimeRec, comp.lo, &FileInfo.modDateTime);
FileAttr |= ATTR_MODTIME;
haveTime = 1;
}

free(pstring);
}
}
Expand Down Expand Up @@ -542,6 +602,9 @@ int do_http(const char *url, URLComponents *components)
FILE *file;

file = stdout;

FileAttr = 0;
memset(&FileInfo, 0, sizeof(FileInfo));

if (!components->portNumber) components->portNumber = 80;

Expand Down Expand Up @@ -575,6 +638,8 @@ int do_http(const char *url, URLComponents *components)
if (path)
{
// path starts with /.

// todo -- also need to strip any ? parameters.

filename = strrchr(path + 1, '/');
if (filename) // *filename == '/'
Expand Down Expand Up @@ -618,9 +683,11 @@ int do_http(const char *url, URLComponents *components)
return -1;
}

// should set from mime type?
setfiletype(filename);

// hmm, flag for this vs content type?
if (parse_extension_c(filename, &FileInfo.fileType, &FileInfo.auxType))
{
FileAttr |= ATTR_FILETYPE | ATTR_AUXTYPE;
}
}


Expand All @@ -646,6 +713,8 @@ int do_http(const char *url, URLComponents *components)
fflush(file);
if (file != stdout) fclose(file);

if (filename) setfileattr(filename, &FileInfo, FileAttr);

CloseLoop(&connection);
free(host);
free(path);
Expand Down
7 changes: 4 additions & 3 deletions makefile.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CFLAGS += $(DEFINES) -v -w
OBJS = main.o gopher.o url.o connection.o readline2.o scheme.o ftype.o setftype.o \
s16debug.o common.o http.o http.utils.o dictionary.o flags.o \
time.o
OBJS = main.o gopher.o url.o connection.o readline2.o scheme.o ftype.o \
mime.o setftype.o s16debug.o common.o http.o http.utils.o \
dictionary.o flags.o time.o

gopher: $(OBJS)
$(CC) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $@
Expand Down Expand Up @@ -34,6 +34,7 @@ dictionary.o: dictionary.c dictionary.h
setftype.o: setftype.c
scheme.o: scheme.c url.h
ftype.o: ftype.c
mime.o: mime.c

time.o: time.c

Expand Down
Loading

0 comments on commit fff9954

Please sign in to comment.