Skip to content

Instantly share code, notes, and snippets.

@redcap3000
Created April 7, 2012 02:49
Show Gist options
  • Save redcap3000/2324654 to your computer and use it in GitHub Desktop.
Save redcap3000/2324654 to your computer and use it in GitHub Desktop.
JSON/File Loader for G-WAN
// ============================================================================
// This is a Servlet sample for the G-WAN Web Server (http://www.trustleap.com)
// ----------------------------------------------------------------------------
// json_load.c: example uses loadfile.c to load a json text file into the buffer
// Ronaldo Barbachano 4-6-12
// ----------------------------------------------------------------------------
#include "gwan.h" // G-WAN exported functions
#include <string.h>
int main(int argc, char *argv[])
{
xbuf_t *reply = get_reply(argv);
// use ctx incase you want to use another script to process the loaded json
xbuf_ctx buf2;
char *text=0, *record = 0, *jsonCall=0, str[1024];
// maybe just parse the uri
if( argc == 1){
// we're using a ctx buffer (for some reason) Maybe because of the xbuf_frurl (which is basically curl!)
xbuf_init(&buf2);
// get directive .. probably error correct and return error status if this fails?
get_arg("id=", &record, argc, argv);
if(record==0){
return 404;
}
// build uri to call get_arg(id) using modified noheaders to return files from the filesystem
s_snprintf(str, 1023, "?loadfile.c&id=%s", record);
// get json file using local script and record, alternativly load from a char or where
jsonCall = xbuf_frurl(&buf2, "localhost", 8080, HTTP_GET,str, 500, 0);
// import from text
jsn_t *import = jsn_frtext(buf2.ptr, "import");
text = jsn_totext(&buf2, import, 1);
// 1:formatted form , show that we have run jsn_totext properly
xbuf_xcat(reply, "<pre>%s</pre>", text);
// assuming this -should- be disabled if you are continuing to use the ctx?
xbuf_free(&buf2);
return 1;
}
return 404;
}
// ============================================================================
// End of Source Code
// ============================================================================
// ============================================================================
// This is a Servlet sample for the G-WAN Web Server (http://www.trustleap.com)
// ----------------------------------------------------------------------------
// loadfile.c:
// Loads a file based on a GET query without headers,
// returns 404 if id is not found. Designed to be used like an api.
// Check out json_load.c for an example of calling with xbuf_frurl()
// Ronaldo Barbachano 4-6-2012
//
// noheaders.c:
// build a complete HTTP reply -without any response header
// (this useful to send a raw JSON reply for example)
//
// Returning an INVALID HTTP status code in the 1-99 range
// (inclusive) will prevent G-WAN from injecting the missing
// response HTTP Headers.
// ----------------------------------------------------------------------------
#include "gwan.h" // G-WAN exported functions
// ----------------------------------------------------------------------------
// imported functions:
// get_reply(): get a pointer on the 'reply' dynamic buffer from the server
// xbuf_cat(): formatted strcat() (a la printf) in a given dynamic buffer
// ----------------------------------------------------------------------------
// pratice with this script and see if you cannot include it in others and make multiple call
int main(int argc, char *argv[])
{
xbuf_t *reply = get_reply(argv);
xbuf_t f;
char *file = 0, str[1024];
char *wwwpath = (char*)get_env(argv, WWW_ROOT);
if(argc==1){
// get the id argument..
get_arg("id=", &file, argc, argv);
// build string path with wwwpath and s_snprintf, switch out 'json' with something else..
if(file!=0){
s_snprintf(str, 1023, "%s/json/%s", wwwpath, file);
// init dynamic buffer
xbuf_init(&f);
// load file with assembled str
xbuf_frfile(&f, str);
// load succeeded?
if(f.len){
xbuf_ncat(reply,f.ptr, f.len);
xbuf_free(&f);
return 1;
}
xbuf_free(&f);
}
}
// return error because something went wrong (invalid 'id')
return 404;
}
// ============================================================================
// End of Source Code
// ============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment