cgi_register_parse_cb(3)

✨ cgi_register_parse_cb(3) ✨

okay bestie, so this one lives in cgi/cgi.h and it's giving "let me handle my own HTTP data, thanks" πŸ’…


🌸 NAME 🌸

cgi_register_parse_cb β€” register a parse callback πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <cgi/cgi.h>

NEOERR *cgi_register_parse_cb(CGI *cgi, const char *method, const char *ctype,
                              void *rock, CGI_PARSE_CB parse_cb);

πŸ’– ARGUMENTS πŸ’–

  • cgi β€” a CGI struct, the main character 🫢
  • method β€” the HTTP method you wanna handle, or * for all of them (yes, literally all) ✨
  • ctype β€” the HTTP Content-Type you wanna handle, or * for all πŸ’«
  • rock β€” opaque data that we'll pass to your call back. it's your little secret lil package, we don't look inside πŸ™ˆ

πŸ‘‘ DESCRIPTION πŸ‘‘

so the ClearSilver CGI Kit already comes with built-in functionality to handle these methods, no cap:

  • GET β†’ doesn't have any data except the query string, which is processed for all methods πŸ”₯
  • POST w/ application/x-www-form-urlencoded
  • POST w/ multipart/form-data β€” processed as RFC2388 data into files and HDF (peep cgi_filehandle())
  • PUT (any type) β€” the entire data chunk is stored as a file, with meta data in HDF (similar to single files in RFC2388). the data is accessible via cgi_filehandle with NULL for name πŸ‘€

to handle other methods/content types, you have to register your own parse function. this isn't necessary if you aren't expecting any data, and technically HTTP only allows data on PUT/POST requests (and presumably user defined methods). in particular, if you want to implement XML-RPC or SOAP, you'll have to register a callback here to grab the XML data chunk, fr fr.

usually you'll want to register POST w/ application/xml or POST w/ text/xml β€” and like, heads up: you either need to register both, or register POST w/ * and check the ctype yourself. and remember to nerr_raise(CGIParseNotHandled) if you aren't handling the POST 🚨

in general, your callback should:

  1. find out how much data is available:
l = hdf_get_value (cgi->hdf, "CGI.ContentLength", NULL);
len = atoi(l);
  1. and read/handle all of the data using cgiwrap_read.

see the builtin handlers for how this is done πŸ‘‘

πŸ’€ ngl, note that cgiwrap_read is not guaranteed to return all of the data you request (just like fread(3)) since it might be reading off a socket. sorry 😭

you should be careful when reading the data to watch for short reads (ie, end of file) and cases where the client sends you data ad infinitum. like, they will just keep going. forever. protect your peace 🧚


🌈 RETURN VALUE 🌈

None. that's it, that's the whole vibe πŸ’€


⭐ SEE ALSO ⭐


mwah πŸ’‹ that's the page bestie β€” brought to you by ClearSilver, dated 12 July 2007 (yes she's a vintage icon, we stan a legacy legend πŸ‘‘βœ¨). now go register that callback and slay πŸŽ€