Re: embedding web server in cocoa app
Re: embedding web server in cocoa app
- Subject: Re: embedding web server in cocoa app
- From: Sherm Pendley <email@hidden>
- Date: Mon, 16 May 2005 23:16:10 -0400
On May 16, 2005, at 10:59 AM, Nicko van Someren wrote:
Does anyone know of any support code for writing Apache modules in
Objective-C on OS X?
I've been curious about using Objective-C in Apache modules myself,
so I decided to give this a spin. It turns out to be fairly easy,
provided you're familiar with - or willing to learn - the C
interface. I don't know of any "pure Objective-C" interfaces - not
since WebObjects went over to the Dark Side, at least.
Here's a simple "Hello world" module:
/*
hello.c
"Hello world" demo of an Apache module that uses Objective-C
Adapted from a similar C example in O'Reilly's "Writing Apache
Modules With Perl and C"
The Apache module interface is essentially a set of C callback
functions in a bundle. So, this follows the common technique of
handling such callbacks - a C "trampoline" function is registered
to receive the callback, and simply "bounces" the call to an
Objective-C object.
This is a .c file because apxs doesn't know how to handle .m
files. Because it's a .c file, the -ObjC compiler flag is needed.
To compile this, creating hello.so:
apxs -c -Wc,-ObjC -Wl,-lobjc -Wl,-framework -Wl,Foundation
hello.c
To install hello.so into Apache's module directory, and add the
necessary LoadModule/AddModule directives to httpd.conf:
sudo apxs -i -a hello.so
In httpd.conf, or a user's .htaccess, configure a location to use
this handler. For example:
<Location /hey/there>
SetHandler hello-handler
</Location>
Then restart apache, either with the "Sharing" pane in "System
Preferences", or the apachectl tool:
sudo apachectl restart
Now hit your local server:
http://127.0.0.1/hey/there
*/
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#import <Foundation/Foundation.h>
/* I've included the interface here in the .c file for simplicity
Obviously a real-world app would probably use an external interface
imported from a header file.
*/
@interface ApacheHelloHandler : NSObject
{
}
+(id) sharedApacheHelloHandler;
-(int) sayHelloForRequest: (request_rec*)r;
@end
// The single static instance of the handler object
static ApacheHelloHandler *sharedHandler;
@implementation ApacheHelloHandler
+(id) sharedApacheHelloHandler {
/* A more elaborate module could use an application or module
startup
handler to create the shared instance, and an exit handler to
release it.
*/
if (nil == sharedHandler)
sharedHandler = [[self alloc] init];
return sharedHandler;
}
-(int) sayHelloForRequest: (request_rec*)r {
const char* hostname;
r->content_type = "text/plain";
ap_send_http_header(r);
hostname = ap_get_remote_host(r->connection,
r->per_dir_config,
REMOTE_NAME);
ap_rprintf(r, "Hello %s\n", hostname);
return OK;
}
@end
// The handler function
static int hello_handler(request_rec *c) {
return [[ApacheHelloHandler sharedApacheHelloHandler]
sayHelloForRequest: c];
}
// Make the name of the content handler known to apache
static handler_rec hello_handlers[] = {
{"hello-handler", hello_handler},
{NULL}
};
// Tell apache what sort of transactions this module will handle
module MODULE_VAR_EXPORT hello_module = {
STANDARD_MODULE_STUFF,
NULL, // module initializer
NULL, // per-directory config creator
NULL, // dir config merger
NULL, // server config creator
NULL, // server config merger
NULL, // command table
hello_handlers, // content handlers
NULL, // URI-to-filename translation
NULL, // check/validate user id
NULL, // check user_id is valid *here*
NULL, // check access by host address
NULL, // MIME type checker/setter
NULL, // fixups
NULL, // logger
NULL, // header parser
NULL, // process initialization
NULL, // process exit/cleanup
NULL // post read_request handling
};
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden