// ============================================================================ // C servlet example for the G-WAN Web Application Server (http://www.gwan.ch/) // ---------------------------------------------------------------------------- // entity.c: how to handle the POST/PUT/DELETE requests and their entity // // We list the URI parameters and entiry (if any) and we decide what // to do with an entity loaded by G-WAN. // // G-WAN will automatically load any entity up to a certain limit. // Entities largre than the limit are rejected with an HTTP error // 413: "request Entity too large". // // To load entities greater than 4KiB you will have to enlarge the // default maximum entity size (see entity_size.c for an example): // // u32 *max_entity_size = (int*)get_env(argv, MAX_ENTITY_SIZE); // *max_entity_size = 200 * 1024; // in bytes (here 200 KiB) // // Note that the URI parameters and the entity are processed in a // similar way by GET and PUT/POST/DELETE (both URI parameters and // an entity are reported in argc/argv[]). This is done purposely // to have a consistent behavior in all the cases. // ============================================================================ // examples (using command 'GET' from package 'libwww-perl'): // // $ GET -m POST "127.0.0.1:8080/?entity.c" // Please enter content (application/x-www-form-urlencoded) to be POSTed: // 0123456[CRLF][Ctrl+D] // //
// HTTP method: POST // HTTP request: POST /?put.c // HTTP query: put.c // HTTP entity: 0123456[CRLF] // Content-Encoding: 0: IDENTITY (default) // Content-Length: 8 // Content-Type: 1: OCTETSTREAM // by the client: 127.0.0.1 // Req. parameters: 1 // argv[0]: 0123456 //// ---------------------------------------------------------------------------- // $ GET -m PUT "127.0.0.1:8080/?entity.c&toto&titi" // Please enter content (text/plain) to be PUTed: // 0123456[CRLF][Ctrl+D] // //
// HTTP method: PUT // HTTP request: PUT /?entity.c // HTTP query: entity.c // HTTP entity: 0123456[CRLF] // Content-Encoding: 0: IDENTITY (default) // Content-Length: 8 // Content-Type: 0: plain text // by the client: 127.0.0.1 // Req. parameters: 3 // argv[0]: toto // argv[1]: titi // argv[2]: 0123456 //// ---------------------------------------------------------------------------- // $ GET -m DELETE "127.0.0.1:8080/?entity.c&toto&titi" // //
// HTTP method: DELETE // HTTP request: DELETE /?entity.c // HTTP query: entity.c // HTTP entity: (null) // Content-Encoding: 0: IDENTITY (default) // Content-Length: 0 // Content-Type: 0: plain text // by the client: 127.0.0.1 // Req. parameters: 2 // argv[0]: toto // argv[1]: titi //// ---------------------------------------------------------------------------- // example using wget: // // wget --post-file="127.0.0.1_8080/#127.0.0.1/www/100.html" "http://127.0.0.1:8080/?entity" // // POST /?entity HTTP/1.0 // User-Agent: Wget/1.12 (linux-gnu) // Accept: */* // Host: 127.0.0.1:8080 // Connection: Keep-Alive // Content-Type: application/x-www-form-urlencoded // Content-Length: 100 // //
// HTTP method: POST // HTTP request: POST /?entity // HTTP query: entity // HTTP entity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // // Content-Encoding: 0: IDENTITY (default) // Content-Length: 100 // Content-Type: 1: OCTETSTREAM // by the client: 127.0.0.1 // Req. parameters: 1 // argv[0]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // //
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX //
\n" "HTTP method: %s\n" "HTTP request: %s\n" "HTTP query: %s\n" "HTTP entity: '%.54s'%s\n" "Content-Encoding: %u: %s\n" "Content-Length: %U\n" "Content-Type: %u: %s\n" "by the client: %s\n" "Req. parameters: %d\n", szHTTP_Method[method], request, query, entity, cont_len > 54 ? "..." : "", cont_enc, szenc, cont_len, cont_type, sztype, cli_ip, argc); int i = 0; while(i < argc) { xbuf_xcat(reply, " argv[%u]: '%.54s'\n", i, argv[i]); i++; } xbuf_cat(reply, "
\n%*v\n%s\n
\n", sizeof("\n") - 1); xbuf_ncat(reply, entity, to_dump); xbuf_xcat(reply, "\n%s
\n", cont_len > to_dump ? ".\n.\n." : ""); // escape '<' because it cuts the text while(xbuf_replfrto(reply, pos, reply->ptr + reply->len - 13, "<", "<")); return HTTP_200_OK; // return an HTTP code } // ============================================================================ // End of Source Code // ============================================================================