Add method for vending static pages.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@20468 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2004-12-19 06:15:55 +00:00
parent 12c69e8a6e
commit 960f13e642
3 changed files with 98 additions and 2 deletions

View file

@ -1,3 +1,8 @@
Sat Dec 18 06:00:00 2004 Richard Frith-Macdonald <rfm@gnu.org>
* WebServer.m: Fix bug in substitution of nil values into templates.
Add new method to vend static pages.
Wed Dec 15 13:10:00 2004 Richard Frith-Macdonald <rfm@gnu.org>
* MySQL.m, Postgres.m, ECPG.pgm: Do NSLog() logging of field

View file

@ -266,6 +266,19 @@
from: (NSDictionary*)params
charset: (NSString*)charset;
/**
* Loads a template file from disk and places it in aResponse as content
* whose mime type is determined from the file extension using the
* provided mapping (or a simple built-in default mapping if map is nil).<br />
* If you have a dedicated web server for handling static pages (eg images)
* it is better to use that rather than vending static pages using this
* method. It's unlikley that this method can be as efficient as a dedicated
* server. However this mechanism is adequate for moderate throughputs.
*/
- (BOOL) produceResponse: (GSMimeDocument*)aResponse
fromStaticPage: (NSString*)aPath
using: (NSDictionary*)map;
/**
* Loads a template file from disk and places it in aResponse as content
* of type 'text/html' with a charset of 'utf-8'.<br />

View file

@ -355,7 +355,7 @@ unescapeData(const unsigned char* bytes, unsigned length, unsigned char *buf)
- (NSString*) description
{
return [NSString stringWithFormat:
@"%@ on %@, %u of %u sessions active, %u total, %u requests, listening: %@",
@"%@ on %@, %u of %u sessions active, %u ended, %u requests, listening: %@",
[super description], _port, NSCountMapTable(_sessions),
_maxSessions, _handled, _requests, _accepting == YES ? @"yes" : @"no"];
}
@ -393,6 +393,84 @@ unescapeData(const unsigned char* bytes, unsigned length, unsigned char *buf)
return YES;
}
- (BOOL) produceResponse: (GSMimeDocument*)aResponse
fromStaticPage: (NSString*)aPath
using: (NSDictionary*)map
{
CREATE_AUTORELEASE_POOL(arp);
NSString *path = (_root == nil) ? (id)@"" : (id)_root;
NSString *ext = [aPath pathExtension];
NSString *type;
NSString *str;
id data;
NSFileManager *mgr;
BOOL string = NO;
BOOL result = YES;
if (map == nil)
{
static NSDictionary *defaultMap = nil;
if (defaultMap == nil)
{
defaultMap = [[NSDictionary alloc] initWithObjectsAndKeys:
@"image/gif", @"gif",
@"image/png", @"png",
@"image/jpeg", @"jpeg",
@"text/html", @"html",
@"text/plain", @"txt",
@"text/xml", @"xml",
nil];
}
map = defaultMap;
}
type = [map objectForKey: ext];
if (type == nil)
{
type = [map objectForKey: [ext lowercaseString]];
}
if (type == nil)
{
type = @"application/octet-stream";
}
string = [type hasPrefix: @"text/"];
path = [path stringByAppendingString: @"/"];
str = [path stringByStandardizingPath];
path = [path stringByAppendingPathComponent: aPath];
path = [path stringByStandardizingPath];
mgr = [NSFileManager defaultManager];
if ([path hasPrefix: str] == NO)
{
[self _alert: @"Illegal static page '%@' ('%@')", aPath, path];
result = NO;
}
else if ([mgr isReadableFileAtPath: path] == NO)
{
[self _alert: @"Can't read static page '%@' ('%@')", aPath, path];
result = NO;
}
else if (string == YES
&& (data = [NSString stringWithContentsOfFile: path]) == nil)
{
[self _alert: @"Failed to load string '%@' ('%@')", aPath, path];
result = NO;
}
else if (string == NO
&& (data = [NSData dataWithContentsOfFile: path]) == nil)
{
[self _alert: @"Failed to load data '%@' ('%@')", aPath, path];
result = NO;
}
else
{
[aResponse setContent: data type: type name: nil];
}
DESTROY(arp);
return result;
}
- (BOOL) produceResponse: (GSMimeDocument*)aResponse
fromTemplate: (NSString*)aPath
using: (NSDictionary*)map
@ -717,7 +795,7 @@ unescapeData(const unsigned char* bytes, unsigned length, unsigned char *buf)
r = NSMakeRange(pos, r.location - pos);
[result appendString: [aTemplate substringWithRange: r]];
}
pos = r.location;
pos = start;
r = NSMakeRange(start + 4, length - start - 4);
r = [aTemplate rangeOfString: @"-->"
options: NSLiteralSearch