public
Description: A cross-platform web server that's scripted with Nu.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nunja.git
Only wrap text/html results in html tags.
timburks (author)
Sat Jun 14 21:55:32 -0700 2008
commit  519b58a154a1a653c41463bf1c723986f0090835
tree    3356090ea7898b862ffb00eddbfbd5de2d20bf1a
parent  c8bb0581af61ec742ecbad5ab741dc9f5b34a191
...
149
150
151
 
 
152
153
154
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
179
180
...
149
150
151
152
153
154
155
156
...
165
166
167
 
 
 
 
 
 
 
 
 
 
 
 
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
0
@@ -149,6 +149,8 @@
0
                         (else nil)))
0
             (else nil)))
0
      
0
+ (set text-html-pattern (regex "^text/html.*$"))
0
+
0
      ;; Handle a request. Used internally.
0
      (- (id)handleRequest:(id)request is
0
         (if (Nunja verbose)
0
@@ -163,18 +165,28 @@
0
         
0
         (unless BODY (return)) ;; return early and leave connection open, this expects the handler to have created a closure
0
         
0
- (if (BODY isKindOfClass:NSString)
0
- (then (set html "<html>\n<head>\n")
0
- (if (response "HEAD")
0
- (then (html appendString:(response "HEAD")))
0
- (else (if HEAD (html appendString:HEAD))))
0
- (if (response "TITLE")
0
- (then (html appendString:(+ "\n<title>" (response "TITLE") "</title>")))
0
- (else (if TITLE (html appendString:(+ "\n<title>" TITLE "</title>")))))
0
- (html appendString: (+ "</head>\n<body>\n" BODY "\n</body>\n</html>\n"))
0
- (request respondWithString:html))
0
- (else (request respondWithData:BODY))))
0
-
0
+ (cond ;; return data objects as-is
0
+ ((BODY isKindOfClass:NSData)
0
+ (request respondWithData:BODY))
0
+ ;; return non-strings as their stringValues
0
+ ((not (BODY isKindOfClass:NSString))
0
+ (request respondWithString:(BODY stringValue)))
0
+ ;; if a content type is set and it isn't text/html, return string as-is
0
+ ((and (set content-type (request valueForResponseHeader:"Content-Type"))
0
+ (not (text-html-pattern findInString:content-type)))
0
+ (request respondWithString:BODY))
0
+ ;; otherwise, return the string wrapped in html tags
0
+ (else
0
+ (set html "<html>\n<head>\n")
0
+ (if (response "HEAD")
0
+ (then (html appendString:(response "HEAD")))
0
+ (else (if HEAD (html appendString:HEAD))))
0
+ (if (response "TITLE")
0
+ (then (html appendString:(+ "\n<title>" (response "TITLE") "</title>")))
0
+ (else (if TITLE (html appendString:(+ "\n<title>" TITLE "</title>")))))
0
+ (html appendString: (+ "</head>\n<body>\n" BODY "\n</body>\n</html>\n"))
0
+ (request respondWithString:html))))
0
+
0
      ;; Return a response redirecting the client to a new location. This method may be called from action handlers.
0
      (- (id)redirectResponse:(id)request toLocation:(id)location is
0
         (request setValue:location forResponseHeader:"Location")
0
...
42
43
44
45
46
47
48
 
 
 
 
 
 
49
50
51
...
42
43
44
 
 
 
 
45
46
47
48
49
50
51
52
53
0
@@ -42,10 +42,12 @@
0
 ;; process the remaining arguments
0
 (while (< argi (argv count))
0
        (case (argv argi)
0
- ("-s" (set argi (+ argi 1)) (set site (argv argi)))
0
- ("--site" (set argi (+ argi 1)) (set site (argv argi)))
0
- ("-p" (set argi (+ argi 1)) (set port ((argv argi) intValue)))
0
- ("--port" (set argi (+ argi 1)) (set port ((argv argi) intValue)))
0
+ ("-s" (set argi (+ argi 1)) (set site (argv argi)))
0
+ ("--site" (set argi (+ argi 1)) (set site (argv argi)))
0
+ ("-p" (set argi (+ argi 1)) (set port ((argv argi) intValue)))
0
+ ("--port" (set argi (+ argi 1)) (set port ((argv argi) intValue)))
0
+ ("-v" (Nunja setVerbose:YES))
0
+ ("--verbose" (Nunja setVerbose:YES))
0
              (else (puts (+ "unknown option: " (argv argi)))
0
                    (exit -1)))
0
        (set argi (+ argi 1)))
...
40
41
42
43
 
44
45
46
...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
...
136
137
138
 
 
 
 
 
 
139
140
141
...
196
197
198
199
 
200
201
202
203
204
 
205
206
207
...
40
41
42
 
43
44
45
46
...
104
105
106
 
 
 
 
 
 
 
 
 
107
108
109
...
127
128
129
130
131
132
133
134
135
136
137
138
...
193
194
195
 
196
197
198
199
200
 
201
202
203
204
0
@@ -40,7 +40,7 @@ void NunjaInit()
0
 
0
 @class Nunja;
0
 
0
-static bool verbose_nunja = false;
0
+static BOOL verbose_nunja = NO;
0
 
0
 @interface NunjaRequest : NSObject
0
 {
0
@@ -104,15 +104,6 @@ static NSDictionary *nunja_request_headers_helper(struct evhttp_request *req)
0
 - (NSDictionary *) requestHeaders
0
 {
0
     return nunja_request_headers_helper(req);
0
- /*
0
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
0
- struct evkeyval *header;
0
- TAILQ_FOREACH(header, req->input_headers, next) {
0
- [dict setObject:[NSString stringWithCString:header->value encoding:NSUTF8StringEncoding]
0
- forKey:[NSString stringWithCString:header->key encoding:NSUTF8StringEncoding]];
0
- }
0
- return dict;
0
- */
0
 }
0
 
0
 static NSDictionary *nunja_response_headers_helper(struct evhttp_request *req)
0
@@ -136,6 +127,12 @@ static NSDictionary *nunja_response_headers_helper(struct evhttp_request *req)
0
     return evhttp_add_header(req->output_headers, key, value);
0
 }
0
 
0
+- (NSString *) valueForResponseHeader:(const char *) key
0
+{
0
+ const char *value = evhttp_find_header(req->output_headers, key);
0
+ return value ? [NSString stringWithCString:value encoding:NSUTF8StringEncoding] : nil;
0
+}
0
+
0
 - (int) removeResponseHeader:(const char *) key
0
 {
0
     return evhttp_remove_header(req->output_headers, key);
0
@@ -196,12 +193,12 @@ static void nunja_response_helper(struct evhttp_request *req, int code, NSString
0
 
0
 @implementation Nunja
0
 
0
-+ (void) setVerbose:(bool) v
0
++ (void) setVerbose:(BOOL) v
0
 {
0
     verbose_nunja = v;
0
 }
0
 
0
-+ (bool) verbose {return verbose_nunja;}
0
++ (BOOL) verbose {return verbose_nunja;}
0
 
0
 + (void) load
0
 {
...
76
77
78
79
 
80
81
82
...
197
198
199
 
200
 
 
 
 
 
 
...
76
77
78
 
79
80
81
82
...
197
198
199
200
201
202
203
204
205
206
207
0
@@ -76,7 +76,7 @@ HTML)
0
                         (set sessionCookie (NunjaCookie cookieForUser:username))
0
                         (sessionCookies setObject:sessionCookie forKey:(sessionCookie value))
0
                         (REQUEST setValue:(sessionCookie stringValue) forResponseHeader:"Set-Cookie")
0
- (Nunja redirectResponse:REQUEST toLocation:"/login"))
0
+ (Nunja redirectResponse:REQUEST toLocation:"/"))
0
                    (else
0
                         (RESPONSE setValue:"Please try again" forKey:"TITLE")
0
                         <<-HTML
0
@@ -197,4 +197,11 @@ END)
0
               (else (REQUEST respondWithString:"unable to resolve host #{host}")))))
0
      nil) ;; return nil to leave the connection open
0
 
0
+(global upload-count 0)
0
 
0
+(post "/upload"
0
+ (REQUEST setValue:"text/plain" forResponseHeader:"Content-Type")
0
+ (set response "uploading item #{(global upload-count (+ upload-count 1))}\n#{((NSDate date) description)}\n")
0
+ (response appendString:((REQUEST requestHeaders) description))
0
+ (response appendString:"\n")
0
+ response)

Comments

    No one has commented yet.