candle 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.md +4 -0
- data/VERSION +1 -0
- data/bin/candle +21 -0
- data/lib/candle.rb +68 -0
- data/lib/candle/command.rb +27 -0
- data/lib/candle/generators/actions.rb +183 -0
- data/lib/candle/generators/blank.rb +127 -0
- data/lib/candle/generators/cli.rb +55 -0
- data/lib/candle/generators/help.rb +47 -0
- data/lib/candle/generators/jam.rb +80 -0
- data/lib/candle/generators/lua/scripts/AppDelegate.lua +36 -0
- data/lib/candle/generators/lua/scripts/tests/init.lua +6 -0
- data/lib/candle/generators/lua/scripts/tests/someTest.lua +12 -0
- data/lib/candle/generators/lua/wax/ProtocolLoader.h +12 -0
- data/lib/candle/generators/lua/wax/bin/hammer +157 -0
- data/lib/candle/generators/lua/wax/bin/update-xibs +131 -0
- data/lib/candle/generators/lua/wax/bin/waxsim +0 -0
- data/lib/candle/generators/lua/wax/lib/build-scripts/compile-stdlib.sh +14 -0
- data/lib/candle/generators/lua/wax/lib/build-scripts/copy-scripts.sh +58 -0
- data/lib/candle/generators/lua/wax/lib/build-scripts/luac.lua +80 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.h +12 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.m +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.h +12 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.m +251 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.h +14 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.m +240 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.h +54 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.m +304 -0
- data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.h +9 -0
- data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.m +273 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/Rakefile +10 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.c +304 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.h +11 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl-1.0.9.tar.gz +0 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_common.h +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_gen.h +159 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_parse.h +193 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl.c +159 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.c +65 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.h +50 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.c +119 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.h +73 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_bytestack.h +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_common.h +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.c +188 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.h +50 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.c +322 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.h +159 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.c +737 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.h +133 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parse.h +193 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.c +448 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.h +82 -0
- data/lib/candle/generators/lua/wax/lib/lua/lapi.c +1087 -0
- data/lib/candle/generators/lua/wax/lib/lua/lapi.h +16 -0
- data/lib/candle/generators/lua/wax/lib/lua/lauxlib.c +652 -0
- data/lib/candle/generators/lua/wax/lib/lua/lauxlib.h +174 -0
- data/lib/candle/generators/lua/wax/lib/lua/lbaselib.c +653 -0
- data/lib/candle/generators/lua/wax/lib/lua/lcode.c +839 -0
- data/lib/candle/generators/lua/wax/lib/lua/lcode.h +76 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldblib.c +397 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldebug.c +638 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldebug.h +33 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldo.c +518 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldo.h +57 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldump.c +164 -0
- data/lib/candle/generators/lua/wax/lib/lua/lfunc.c +174 -0
- data/lib/candle/generators/lua/wax/lib/lua/lfunc.h +34 -0
- data/lib/candle/generators/lua/wax/lib/lua/lgc.c +711 -0
- data/lib/candle/generators/lua/wax/lib/lua/lgc.h +110 -0
- data/lib/candle/generators/lua/wax/lib/lua/linit.c +38 -0
- data/lib/candle/generators/lua/wax/lib/lua/liolib.c +553 -0
- data/lib/candle/generators/lua/wax/lib/lua/llex.c +461 -0
- data/lib/candle/generators/lua/wax/lib/lua/llex.h +81 -0
- data/lib/candle/generators/lua/wax/lib/lua/llimits.h +128 -0
- data/lib/candle/generators/lua/wax/lib/lua/lmathlib.c +263 -0
- data/lib/candle/generators/lua/wax/lib/lua/lmem.c +86 -0
- data/lib/candle/generators/lua/wax/lib/lua/lmem.h +49 -0
- data/lib/candle/generators/lua/wax/lib/lua/loadlib.c +666 -0
- data/lib/candle/generators/lua/wax/lib/lua/lobject.c +214 -0
- data/lib/candle/generators/lua/wax/lib/lua/lobject.h +381 -0
- data/lib/candle/generators/lua/wax/lib/lua/lopcodes.c +102 -0
- data/lib/candle/generators/lua/wax/lib/lua/lopcodes.h +268 -0
- data/lib/candle/generators/lua/wax/lib/lua/loslib.c +243 -0
- data/lib/candle/generators/lua/wax/lib/lua/lparser.c +1339 -0
- data/lib/candle/generators/lua/wax/lib/lua/lparser.h +82 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstate.c +214 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstate.h +169 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstring.c +111 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstring.h +31 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstrlib.c +869 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltable.c +588 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltable.h +40 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltablib.c +287 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltm.c +75 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltm.h +54 -0
- data/lib/candle/generators/lua/wax/lib/lua/lua.h +388 -0
- data/lib/candle/generators/lua/wax/lib/lua/luaconf.h +753 -0
- data/lib/candle/generators/lua/wax/lib/lua/lualib.h +53 -0
- data/lib/candle/generators/lua/wax/lib/lua/lundump.c +227 -0
- data/lib/candle/generators/lua/wax/lib/lua/lundump.h +36 -0
- data/lib/candle/generators/lua/wax/lib/lua/lvm.c +763 -0
- data/lib/candle/generators/lua/wax/lib/lua/lvm.h +36 -0
- data/lib/candle/generators/lua/wax/lib/lua/lzio.c +82 -0
- data/lib/candle/generators/lua/wax/lib/lua/lzio.h +67 -0
- data/lib/candle/generators/lua/wax/lib/lua/print.c +227 -0
- data/lib/candle/generators/lua/wax/lib/project.rake +159 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/enums.lua +396 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/http.lua +43 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/init.lua +4 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/number.lua +21 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/string.lua +97 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/table.lua +165 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/WaxServer.lua +49 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/autoload.lua +10 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/base64.lua +64 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/bit.lua +274 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/cache.lua +73 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/callback.lua +22 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/frame.lua +76 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/init.lua +78 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/pickView.lua +54 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/time.lua +102 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/init.lua +18 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/init.lua +2 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luamock.lua +84 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luaspec.lua +377 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/repl.lua +9 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/structs.lua +11 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/waxClass.lua +42 -0
- data/lib/candle/generators/lua/wax/lib/wax.h +16 -0
- data/lib/candle/generators/lua/wax/lib/wax.m +260 -0
- data/lib/candle/generators/lua/wax/lib/wax_class.h +18 -0
- data/lib/candle/generators/lua/wax/lib/wax_class.m +190 -0
- data/lib/candle/generators/lua/wax/lib/wax_gc.h +20 -0
- data/lib/candle/generators/lua/wax/lib/wax_gc.m +56 -0
- data/lib/candle/generators/lua/wax/lib/wax_helpers.h +102 -0
- data/lib/candle/generators/lua/wax/lib/wax_helpers.m +870 -0
- data/lib/candle/generators/lua/wax/lib/wax_instance.h +34 -0
- data/lib/candle/generators/lua/wax/lib/wax_instance.m +810 -0
- data/lib/candle/generators/lua/wax/lib/wax_server.h +47 -0
- data/lib/candle/generators/lua/wax/lib/wax_server.m +252 -0
- data/lib/candle/generators/lua/wax/lib/wax_stdlib.h +3 -0
- data/lib/candle/generators/lua/wax/lib/wax_struct.h +26 -0
- data/lib/candle/generators/lua/wax/lib/wax_struct.m +335 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.pbxproj +836 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/WaxApplication.xcscheme +86 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/xcschememanagement.plist +22 -0
- data/lib/candle/generators/templates/blank/WaxApplication/Default-568h@2x.png +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication/Default.png +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication/Default@2x.png +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication/ProtocolLoader.h +12 -0
- data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Info.plist.tt +38 -0
- data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Prefix.pch.tt +14 -0
- data/lib/candle/generators/templates/blank/WaxApplication/en.lproj/InfoPlist.strings +2 -0
- data/lib/candle/generators/templates/blank/WaxApplication/main.m.tt +20 -0
- data/lib/candle/tasks.rb +22 -0
- data/lib/candle/utility.rb +30 -0
- data/lib/candle/version.rb +9 -0
- data/lib/candle/view.rb +48 -0
- metadata +582 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
//
|
2
|
+
// wax_http.h
|
3
|
+
// Rentals
|
4
|
+
//
|
5
|
+
// Created by ProbablyInteractive on 7/13/09.
|
6
|
+
// Copyright 2009 Probably Interactive. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#import <UIKit/UIKit.h>
|
10
|
+
#import "lua.h"
|
11
|
+
|
12
|
+
#define WAX_HTTP_METATABLE_NAME "wax.http"
|
13
|
+
|
14
|
+
int luaopen_wax_http(lua_State *L);
|
@@ -0,0 +1,240 @@
|
|
1
|
+
//
|
2
|
+
// wax_http.m
|
3
|
+
// Rentals
|
4
|
+
//
|
5
|
+
// Created by ProbablyInteractive on 7/13/09.
|
6
|
+
// Copyright 2009 Probably Interactive. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#import "wax_http.h"
|
10
|
+
#import "wax_http_connection.h"
|
11
|
+
#import "wax_instance.h"
|
12
|
+
#import "wax_helpers.h"
|
13
|
+
|
14
|
+
#import "lua.h"
|
15
|
+
#import "lauxlib.h"
|
16
|
+
|
17
|
+
const NSTimeInterval WAX_HTTP_TIMEOUT = 30;
|
18
|
+
|
19
|
+
static int request(lua_State *L);
|
20
|
+
|
21
|
+
static BOOL pushCallback(lua_State *L, char *callbackName, int table_index);
|
22
|
+
|
23
|
+
static int getFormat(lua_State *L, int tableIndex);
|
24
|
+
static NSURLRequestCachePolicy getCachePolicy(lua_State *L, int tableIndex);
|
25
|
+
static NSDictionary *getHeaders(lua_State *L, int tableIndex);
|
26
|
+
static NSString *getMethod(lua_State *L, int tableIndex);
|
27
|
+
static NSTimeInterval getTimeout(lua_State *L, int tableIndex);
|
28
|
+
static NSString *getBody(lua_State *L, int tableIndex);
|
29
|
+
|
30
|
+
static const struct luaL_Reg metaFunctions[] = {
|
31
|
+
{NULL, NULL}
|
32
|
+
};
|
33
|
+
|
34
|
+
static const struct luaL_Reg functions[] = {
|
35
|
+
{"request", request},
|
36
|
+
{NULL, NULL}
|
37
|
+
};
|
38
|
+
|
39
|
+
int luaopen_wax_http(lua_State *L) {
|
40
|
+
BEGIN_STACK_MODIFY(L);
|
41
|
+
|
42
|
+
luaL_newmetatable(L, WAX_HTTP_METATABLE_NAME);
|
43
|
+
luaL_register(L, NULL, metaFunctions);
|
44
|
+
luaL_register(L, WAX_HTTP_METATABLE_NAME, functions);
|
45
|
+
|
46
|
+
lua_pushvalue(L, -2);
|
47
|
+
lua_setmetatable(L, -2); // Set the metatable for the module
|
48
|
+
|
49
|
+
END_STACK_MODIFY(L, 0)
|
50
|
+
|
51
|
+
return 0;
|
52
|
+
}
|
53
|
+
|
54
|
+
// wax.request({url, options}) => returns connection object or (body, response) if syncronous
|
55
|
+
// wax.request{url, options} => same as above, but with syntax sugar
|
56
|
+
// options:
|
57
|
+
// method = "get" | "post" | "put" | "delete"
|
58
|
+
// format = "text" | "binary" | "json" | "xml" # if none given, uses value from response Content-Type Header
|
59
|
+
// headers = table # Table of header values
|
60
|
+
// timout = number
|
61
|
+
// body = string
|
62
|
+
// cache = NSURLRequestCachePolicy # one of those enums, defaults to NSURLRequestUseProtocolCachePolicy
|
63
|
+
// callback = function(body, response) # No callback? Then treat request is treated as syncronous
|
64
|
+
// progressCallback = function(percentComplete, data) # Shows how much is left to download
|
65
|
+
// authCallback = function(NSURLAuthenticationChallenge) # Handle just like you would with NSURLConnection
|
66
|
+
// redirectCallback = function(response) # If there is a redirect, this callback will be called
|
67
|
+
static int request(lua_State *L) {
|
68
|
+
lua_rawgeti(L, 1, 1);
|
69
|
+
|
70
|
+
NSString *urlString = [NSString stringWithUTF8String:luaL_checkstring(L, -1)];
|
71
|
+
if (![urlString hasPrefix:@"http://"] && ![urlString hasPrefix:@"https://"]) urlString = [NSString stringWithFormat:@"http://%@", urlString];
|
72
|
+
NSURL *url = [NSURL URLWithString:urlString];
|
73
|
+
|
74
|
+
lua_pop(L, 1); // Pop the url off the stack
|
75
|
+
|
76
|
+
if (!url) luaL_error(L, "wax_http: Could not create URL from string '%s'", [urlString UTF8String]);
|
77
|
+
|
78
|
+
|
79
|
+
NSURLRequestCachePolicy cachePolicy = getCachePolicy(L, 1);
|
80
|
+
NSDictionary *headerFields = getHeaders(L, 1);
|
81
|
+
NSData *body = [getBody(L, 1) dataUsingEncoding:NSUTF8StringEncoding];
|
82
|
+
|
83
|
+
// Get the format
|
84
|
+
int format = getFormat(L, 1);
|
85
|
+
NSTimeInterval timeout = getTimeout(L, 1);
|
86
|
+
NSString *method = getMethod(L, 1);
|
87
|
+
|
88
|
+
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
|
89
|
+
|
90
|
+
[urlRequest setHTTPMethod:method];
|
91
|
+
[urlRequest setCachePolicy:cachePolicy];
|
92
|
+
[urlRequest setAllHTTPHeaderFields:headerFields];
|
93
|
+
[urlRequest setHTTPBody:body];
|
94
|
+
|
95
|
+
wax_http_connection *connection;
|
96
|
+
connection = [[[wax_http_connection alloc] initWithRequest:urlRequest timeout:timeout luaState:L] autorelease];
|
97
|
+
connection.format = format;
|
98
|
+
[urlRequest release];
|
99
|
+
|
100
|
+
wax_instance_create(L, connection, NO);
|
101
|
+
|
102
|
+
if (pushCallback(L, WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME, 1)) {
|
103
|
+
lua_setfield(L, -2, WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME); // Set the authCallback function for the userdata
|
104
|
+
}
|
105
|
+
|
106
|
+
if (pushCallback(L, WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME, 1)) {
|
107
|
+
lua_setfield(L, -2, WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME); // Set the progressCallback function for the userdata
|
108
|
+
}
|
109
|
+
|
110
|
+
if (pushCallback(L, WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME, 1)) {
|
111
|
+
lua_setfield(L, -2, WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME); // Set the redirectCallback function for the userdata
|
112
|
+
}
|
113
|
+
|
114
|
+
// Asyncronous or Syncronous
|
115
|
+
if (pushCallback(L, WAX_HTTP_CALLBACK_FUNCTION_NAME, 1)) {
|
116
|
+
lua_setfield(L, -2, WAX_HTTP_CALLBACK_FUNCTION_NAME); // Set the callback function for the userdata
|
117
|
+
|
118
|
+
[connection start];
|
119
|
+
|
120
|
+
return 1; // Return the connectionDelegate as userdata
|
121
|
+
}
|
122
|
+
else {
|
123
|
+
[connection start];
|
124
|
+
|
125
|
+
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
|
126
|
+
while (!connection.finished) {
|
127
|
+
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
|
128
|
+
}
|
129
|
+
|
130
|
+
return 3;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
static NSTimeInterval getTimeout(lua_State *L, int tableIndex) {
|
135
|
+
NSTimeInterval timeout = WAX_HTTP_TIMEOUT; // Default
|
136
|
+
if (lua_isnoneornil(L, tableIndex)) return timeout;
|
137
|
+
|
138
|
+
lua_getfield(L, tableIndex, "timeout");
|
139
|
+
if (!lua_isnil(L, -1)) {
|
140
|
+
timeout = luaL_checknumber(L, -1);
|
141
|
+
}
|
142
|
+
lua_pop(L, 1);
|
143
|
+
|
144
|
+
return timeout;
|
145
|
+
}
|
146
|
+
|
147
|
+
static NSDictionary *getHeaders(lua_State *L, int tableIndex) {
|
148
|
+
NSDictionary *headers = [NSDictionary dictionary];
|
149
|
+
|
150
|
+
if (lua_isnoneornil(L, tableIndex)) return headers;
|
151
|
+
|
152
|
+
lua_getfield(L, tableIndex, "headers");
|
153
|
+
if (!lua_isnil(L, -1)) {
|
154
|
+
id *result = wax_copyToObjc(L, "@", -1, nil);
|
155
|
+
headers = *result;
|
156
|
+
free(result);
|
157
|
+
}
|
158
|
+
lua_pop(L, 1);
|
159
|
+
|
160
|
+
return headers;
|
161
|
+
}
|
162
|
+
|
163
|
+
static NSString *getMethod(lua_State *L, int tableIndex) {
|
164
|
+
NSString *method = @"GET"; // Default
|
165
|
+
if (lua_isnoneornil(L, tableIndex)) return method;
|
166
|
+
|
167
|
+
lua_getfield(L, tableIndex, "method");
|
168
|
+
if (!lua_isnil(L, -1)) {
|
169
|
+
const char *string = luaL_checkstring(L, -1);
|
170
|
+
method = [[NSString stringWithUTF8String:string] uppercaseString];
|
171
|
+
}
|
172
|
+
lua_pop(L, 1);
|
173
|
+
|
174
|
+
return method;
|
175
|
+
}
|
176
|
+
|
177
|
+
static NSURLRequestCachePolicy getCachePolicy(lua_State *L, int tableIndex) {
|
178
|
+
NSURLRequestCachePolicy cachePolicy = NSURLRequestUseProtocolCachePolicy;
|
179
|
+
if (lua_isnoneornil(L, tableIndex)) return cachePolicy;
|
180
|
+
|
181
|
+
lua_getfield(L, tableIndex, "cache");
|
182
|
+
if (!lua_isnil(L, -1)) {
|
183
|
+
cachePolicy = luaL_checknumber(L, -1);
|
184
|
+
}
|
185
|
+
lua_pop(L, 1);
|
186
|
+
|
187
|
+
return cachePolicy;
|
188
|
+
}
|
189
|
+
|
190
|
+
static int getFormat(lua_State *L, int tableIndex) {
|
191
|
+
int format = WAX_HTTP_UNKNOWN; // Default
|
192
|
+
if (lua_isnoneornil(L, tableIndex)) return format;
|
193
|
+
|
194
|
+
lua_getfield(L, tableIndex, "format");
|
195
|
+
if (!lua_isnil(L, -1)) {
|
196
|
+
const char *formatString = luaL_checkstring(L, -1);
|
197
|
+
if (strcasecmp(formatString, "text") == 0) {
|
198
|
+
format = WAX_HTTP_TEXT;
|
199
|
+
}
|
200
|
+
else if (strcasecmp(formatString, "binary") == 0) {
|
201
|
+
format = WAX_HTTP_BINARY;
|
202
|
+
}
|
203
|
+
else if (strcasecmp(formatString, "json") == 0) {
|
204
|
+
format = WAX_HTTP_JSON;
|
205
|
+
}
|
206
|
+
else {
|
207
|
+
luaL_error(L, "wax_http: Unknown format name '%s'", formatString);
|
208
|
+
}
|
209
|
+
}
|
210
|
+
lua_pop(L, 1);
|
211
|
+
|
212
|
+
return format;
|
213
|
+
}
|
214
|
+
|
215
|
+
static NSString *getBody(lua_State *L, int tableIndex) {
|
216
|
+
NSString *body = @""; // Default
|
217
|
+
if (lua_isnoneornil(L, tableIndex)) return body;
|
218
|
+
|
219
|
+
lua_getfield(L, tableIndex, "body");
|
220
|
+
if (!lua_isnil(L, -1)) {
|
221
|
+
const char *string = luaL_checkstring(L, -1);
|
222
|
+
body = [NSString stringWithUTF8String:string];
|
223
|
+
}
|
224
|
+
lua_pop(L, 1);
|
225
|
+
|
226
|
+
return body;
|
227
|
+
}
|
228
|
+
|
229
|
+
// Assumes table is on top of the stack
|
230
|
+
static BOOL pushCallback(lua_State *L, char *callbackName, int tableIndex) {
|
231
|
+
lua_getfield(L, tableIndex, callbackName);
|
232
|
+
|
233
|
+
if (lua_isnil(L, -1)) {
|
234
|
+
lua_pop(L, 1);
|
235
|
+
return NO;
|
236
|
+
}
|
237
|
+
else {
|
238
|
+
return YES;
|
239
|
+
}
|
240
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
//
|
2
|
+
// wax_http_connection.h
|
3
|
+
// RentList
|
4
|
+
//
|
5
|
+
// Created by Corey Johnson on 8/9/09.
|
6
|
+
// Copyright 2009 ProbablyInteractive. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#import <UIKit/UIKit.h>
|
10
|
+
#import "lua.h"
|
11
|
+
|
12
|
+
enum {
|
13
|
+
WAX_HTTP_UNKNOWN,
|
14
|
+
WAX_HTTP_TEXT,
|
15
|
+
WAX_HTTP_BINARY, // Like an image or something
|
16
|
+
WAX_HTTP_JSON,
|
17
|
+
WAX_HTTP_XML
|
18
|
+
};
|
19
|
+
|
20
|
+
#define WAX_HTTP_CALLBACK_FUNCTION_NAME "callback"
|
21
|
+
#define WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME "progressCallback"
|
22
|
+
#define WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME "authCallback"
|
23
|
+
#define WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME "redirectCallback"
|
24
|
+
|
25
|
+
@interface wax_http_connection : NSURLConnection {
|
26
|
+
lua_State *L;
|
27
|
+
NSMutableData *_data;
|
28
|
+
NSHTTPURLResponse *_response;
|
29
|
+
NSURLRequest *_request;
|
30
|
+
NSTimer *_timeoutTimer;
|
31
|
+
NSError *_error;
|
32
|
+
|
33
|
+
NSTimeInterval _timeout;
|
34
|
+
int _format;
|
35
|
+
bool _finished;
|
36
|
+
bool _canceled;
|
37
|
+
}
|
38
|
+
|
39
|
+
@property (nonatomic, assign) NSHTTPURLResponse *response;
|
40
|
+
|
41
|
+
@property (nonatomic, assign) int format;
|
42
|
+
@property (nonatomic, readonly, getter=isFinished) bool finished;
|
43
|
+
|
44
|
+
- (id)initWithRequest:(NSURLRequest *)urlRequest timeout:(NSTimeInterval)timeout luaState:(lua_State *)luaState;
|
45
|
+
- (void)callRedirectCallback:(NSURLResponse *)redirectResponse;
|
46
|
+
- (BOOL)callLuaAuthCallback:(NSURLAuthenticationChallenge *)challenge;
|
47
|
+
- (void)callLuaProgressCallback;
|
48
|
+
- (void)callLuaCallback;
|
49
|
+
|
50
|
+
// HSHTTPURLResponse Delegate Methods
|
51
|
+
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
|
52
|
+
|
53
|
+
@end
|
54
|
+
|
@@ -0,0 +1,304 @@
|
|
1
|
+
//
|
2
|
+
// wax_http_connection.m
|
3
|
+
// RentList
|
4
|
+
//
|
5
|
+
// Created by Corey Johnson on 8/9/09.
|
6
|
+
// Copyright 2009 ProbablyInteractive. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#include <CommonCrypto/CommonDigest.h>
|
10
|
+
|
11
|
+
#import "lauxlib.h"
|
12
|
+
|
13
|
+
#import "wax_http_connection.h"
|
14
|
+
#import "wax_instance.h"
|
15
|
+
#import "wax_helpers.h"
|
16
|
+
#import "wax_json.h"
|
17
|
+
|
18
|
+
#ifdef WAX_XML_INCLUDED
|
19
|
+
#import "wax_xml.h"
|
20
|
+
#endif
|
21
|
+
|
22
|
+
@implementation wax_http_connection
|
23
|
+
|
24
|
+
@synthesize response=_response;
|
25
|
+
@synthesize format=_format;
|
26
|
+
@synthesize finished=_finished;
|
27
|
+
|
28
|
+
- (void)dealloc {
|
29
|
+
[_data release];
|
30
|
+
[_error release];
|
31
|
+
[_response release];
|
32
|
+
[_request release];
|
33
|
+
[_timeoutTimer release];
|
34
|
+
[super dealloc];
|
35
|
+
}
|
36
|
+
|
37
|
+
- (id)initWithRequest:(NSURLRequest *)urlRequest timeout:(NSTimeInterval)timeout luaState:(lua_State *)luaState {
|
38
|
+
self = [super initWithRequest:urlRequest delegate:self startImmediately:NO];
|
39
|
+
[self scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
40
|
+
L = luaState;
|
41
|
+
|
42
|
+
_data = [[NSMutableData alloc] init];
|
43
|
+
_request = [urlRequest retain];
|
44
|
+
|
45
|
+
_timeout = timeout;
|
46
|
+
_format = WAX_HTTP_UNKNOWN;
|
47
|
+
_canceled = NO;
|
48
|
+
return self;
|
49
|
+
}
|
50
|
+
|
51
|
+
- (void)timeoutHack {
|
52
|
+
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:nil];
|
53
|
+
[self connection:self didFailWithError:error];
|
54
|
+
[self cancel];
|
55
|
+
}
|
56
|
+
|
57
|
+
- (void)start {
|
58
|
+
// Apple makes has a mandatory 240 second minimum timeout WTF? https://devforums.apple.com/thread/25282
|
59
|
+
// Because of this stupidity by Apple, we are forced to setup our own timeout
|
60
|
+
// using a timer.
|
61
|
+
_timeoutTimer = [[NSTimer scheduledTimerWithTimeInterval:_timeout target:self selector:@selector(timeoutHack) userInfo:nil repeats:NO] retain];
|
62
|
+
wax_log(LOG_NETWORK, @"HTTP(%@) %@", [_request HTTPMethod], [_request URL]);
|
63
|
+
[super start];
|
64
|
+
}
|
65
|
+
|
66
|
+
- (void)cancel {
|
67
|
+
wax_log(LOG_NETWORK, @"CANCELING (%@) %@", [_request HTTPMethod], [_request URL]);
|
68
|
+
[_timeoutTimer invalidate];
|
69
|
+
_canceled = YES;
|
70
|
+
[super cancel];
|
71
|
+
}
|
72
|
+
|
73
|
+
- (bool)isFinished {
|
74
|
+
return _canceled || _finished;
|
75
|
+
}
|
76
|
+
|
77
|
+
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
78
|
+
if (response != _response) [_response release];
|
79
|
+
_response = [response retain];
|
80
|
+
}
|
81
|
+
|
82
|
+
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
|
83
|
+
if (redirectResponse) [self callRedirectCallback:redirectResponse];
|
84
|
+
return request;
|
85
|
+
}
|
86
|
+
|
87
|
+
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
|
88
|
+
if (![self callLuaAuthCallback:challenge]) {
|
89
|
+
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
|
90
|
+
}
|
91
|
+
}
|
92
|
+
|
93
|
+
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
|
94
|
+
[_data appendData:data];
|
95
|
+
[self callLuaProgressCallback];
|
96
|
+
}
|
97
|
+
|
98
|
+
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
99
|
+
if (!_canceled) {
|
100
|
+
_error = [error retain];
|
101
|
+
[_data release];
|
102
|
+
_data = nil;
|
103
|
+
[self callLuaCallback];
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
|
108
|
+
if (!_canceled) {
|
109
|
+
[self callLuaCallback];
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
- (void)callRedirectCallback:(NSURLResponse *)redirectResponse {
|
114
|
+
BEGIN_STACK_MODIFY(L)
|
115
|
+
|
116
|
+
if (_canceled) {
|
117
|
+
assert("OH NO, URL CONNECTION WAS CANCELED BUT NOT CAUGHT");
|
118
|
+
}
|
119
|
+
|
120
|
+
wax_instance_pushUserdata(L, self);
|
121
|
+
lua_getfield(L, -1, WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME);
|
122
|
+
|
123
|
+
bool hasCallback = YES;
|
124
|
+
if (lua_isnil(L, -1)) {
|
125
|
+
hasCallback = NO;
|
126
|
+
lua_pop(L, 1);
|
127
|
+
}
|
128
|
+
|
129
|
+
wax_fromObjc(L, "@", &redirectResponse);
|
130
|
+
|
131
|
+
if (hasCallback && wax_pcall(L, 1, 0)) {
|
132
|
+
const char* error_string = lua_tostring(L, -1);
|
133
|
+
luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_REDIRECT_CALLBACK_FUNCTION_NAME, error_string);
|
134
|
+
}
|
135
|
+
|
136
|
+
END_STACK_MODIFY(L, 0)
|
137
|
+
}
|
138
|
+
|
139
|
+
- (BOOL)callLuaAuthCallback:(NSURLAuthenticationChallenge *)challenge {
|
140
|
+
BEGIN_STACK_MODIFY(L)
|
141
|
+
|
142
|
+
if (_canceled) {
|
143
|
+
assert("OH NO, URL CONNECTION WAS CANCELED BUT NOT CAUGHT");
|
144
|
+
}
|
145
|
+
|
146
|
+
wax_instance_pushUserdata(L, self);
|
147
|
+
lua_getfield(L, -1, WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME);
|
148
|
+
|
149
|
+
bool hasCallback = YES;
|
150
|
+
if (lua_isnil(L, -1)) {
|
151
|
+
hasCallback = NO;
|
152
|
+
lua_pop(L, 1);
|
153
|
+
}
|
154
|
+
|
155
|
+
wax_fromObjc(L, "@", &challenge);
|
156
|
+
|
157
|
+
if (hasCallback && wax_pcall(L, 1, 0)) {
|
158
|
+
const char* error_string = lua_tostring(L, -1);
|
159
|
+
luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_AUTH_CALLBACK_FUNCTION_NAME, error_string);
|
160
|
+
}
|
161
|
+
|
162
|
+
END_STACK_MODIFY(L, 0)
|
163
|
+
|
164
|
+
return hasCallback;
|
165
|
+
}
|
166
|
+
|
167
|
+
- (void)callLuaProgressCallback {
|
168
|
+
BEGIN_STACK_MODIFY(L)
|
169
|
+
|
170
|
+
if (_canceled) {
|
171
|
+
assert("OH NO, URL CONNECTION WAS CANCELED BUT NOT CAUGHT");
|
172
|
+
}
|
173
|
+
|
174
|
+
wax_instance_pushUserdata(L, self);
|
175
|
+
lua_getfield(L, -1, WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME);
|
176
|
+
|
177
|
+
if (lua_isnil(L, -1)) {
|
178
|
+
lua_pop(L, 1);
|
179
|
+
}
|
180
|
+
else {
|
181
|
+
float percentComplete = 0;
|
182
|
+
|
183
|
+
@try {
|
184
|
+
percentComplete = _data.length / [[[_response allHeaderFields] objectForKey:@"Content-Length"] floatValue];
|
185
|
+
}
|
186
|
+
@catch (NSException * e) {
|
187
|
+
NSLog(@"Error: Couldn't calculate percent Complete");
|
188
|
+
}
|
189
|
+
|
190
|
+
|
191
|
+
wax_fromObjc(L, "f", &percentComplete);
|
192
|
+
wax_fromObjc(L, "@", &_data);
|
193
|
+
|
194
|
+
if (wax_pcall(L, 2, 0)) {
|
195
|
+
const char* error_string = lua_tostring(L, -1);
|
196
|
+
luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_PROGRESS_CALLBACK_FUNCTION_NAME, error_string);
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
END_STACK_MODIFY(L, 0)
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
- (void)callLuaCallback {
|
205
|
+
[_timeoutTimer invalidate];
|
206
|
+
|
207
|
+
BEGIN_STACK_MODIFY(L)
|
208
|
+
|
209
|
+
if (_canceled) {
|
210
|
+
assert("OH NO, URL CONNECTION WAS CANCELED BUT NOT CAUGHT");
|
211
|
+
}
|
212
|
+
|
213
|
+
wax_instance_pushUserdata(L, self);
|
214
|
+
lua_getfield(L, -1, WAX_HTTP_CALLBACK_FUNCTION_NAME);
|
215
|
+
|
216
|
+
bool hasCallback = YES;
|
217
|
+
if (lua_isnil(L, -1)) {
|
218
|
+
hasCallback = NO;
|
219
|
+
lua_pop(L, 1);
|
220
|
+
}
|
221
|
+
|
222
|
+
// Try and guess the format type
|
223
|
+
if (_format == WAX_HTTP_UNKNOWN) {
|
224
|
+
NSString *contentType = [[_response allHeaderFields] objectForKey:@"Content-Type"];
|
225
|
+
|
226
|
+
if ([contentType hasPrefix:@"application/xml"] ||
|
227
|
+
[contentType hasPrefix:@"text/xml"]) {
|
228
|
+
#ifdef WAX_XML_INCLUDED
|
229
|
+
_format = WAX_HTTP_XML;
|
230
|
+
#else
|
231
|
+
_format = WAX_HTTP_TEXT;
|
232
|
+
#endif
|
233
|
+
}
|
234
|
+
else if ([contentType hasPrefix:@"application/json"] ||
|
235
|
+
[contentType hasPrefix:@"text/json"] ||
|
236
|
+
[contentType hasPrefix:@"application/javascript"] ||
|
237
|
+
[contentType hasPrefix:@"text/javascript"]) {
|
238
|
+
_format = WAX_HTTP_JSON;
|
239
|
+
}
|
240
|
+
else if ([contentType hasPrefix:@"image/"] ||
|
241
|
+
[contentType hasPrefix:@"audio/"] ||
|
242
|
+
[contentType hasPrefix:@"application/octet-stream"]) {
|
243
|
+
_format = WAX_HTTP_BINARY;
|
244
|
+
}
|
245
|
+
else {
|
246
|
+
_format = WAX_HTTP_TEXT;
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
if (_error) {
|
251
|
+
lua_pushnil(L);
|
252
|
+
}
|
253
|
+
else if (_format == WAX_HTTP_TEXT || _format == WAX_HTTP_JSON || _format == WAX_HTTP_XML) {
|
254
|
+
NSString *string = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
|
255
|
+
if (!string) string = [[NSString alloc] initWithData:_data encoding:NSISOLatin1StringEncoding];
|
256
|
+
if (!string) string = [[NSString alloc] initWithData:_data encoding:NSNonLossyASCIIStringEncoding];
|
257
|
+
if (!string) string = [[NSString alloc] initWithData:_data encoding:NSNonLossyASCIIStringEncoding];
|
258
|
+
|
259
|
+
|
260
|
+
if (_format == WAX_HTTP_JSON) {
|
261
|
+
json_parseString(L, [string UTF8String]);
|
262
|
+
}
|
263
|
+
else if (_format == WAX_HTTP_XML){
|
264
|
+
#ifdef WAX_XML_INCLUDED
|
265
|
+
wax_xml_parseString(L, [string UTF8String]);
|
266
|
+
#else
|
267
|
+
luaL_error(L, "Trying to parse xml, but xml library not included.");
|
268
|
+
#endif
|
269
|
+
}
|
270
|
+
else {
|
271
|
+
wax_fromObjc(L, "@", &string);
|
272
|
+
}
|
273
|
+
|
274
|
+
|
275
|
+
[string release];
|
276
|
+
}
|
277
|
+
else if (_format == WAX_HTTP_BINARY) {
|
278
|
+
wax_fromObjc(L, "@", &_data);
|
279
|
+
}
|
280
|
+
else {
|
281
|
+
luaL_error(L, "wax_http: Unknown wax_http format '%d'", _format);
|
282
|
+
}
|
283
|
+
|
284
|
+
wax_fromObjc(L, "@", &_response);
|
285
|
+
if (_error) {
|
286
|
+
wax_fromObjc(L, "@", &_error);
|
287
|
+
}
|
288
|
+
else {
|
289
|
+
lua_pushnil(L);
|
290
|
+
}
|
291
|
+
|
292
|
+
|
293
|
+
|
294
|
+
if (hasCallback && wax_pcall(L, 3, 0)) {
|
295
|
+
const char* error_string = lua_tostring(L, -1);
|
296
|
+
luaL_error(L, "Problem calling Lua function '%s' from wax_http.\n%s", WAX_HTTP_CALLBACK_FUNCTION_NAME, error_string);
|
297
|
+
}
|
298
|
+
|
299
|
+
_finished = YES;
|
300
|
+
|
301
|
+
END_STACK_MODIFY(L, 3)
|
302
|
+
}
|
303
|
+
|
304
|
+
@end
|