@@ -53,7 +53,8 @@ HTTPSServer secureServer = HTTPSServer(&cert);
5353// We declare some handler functions (definition at the end of the file)
5454void handleRoot (HTTPRequest * req, HTTPResponse * res);
5555void handleSVG (HTTPRequest * req, HTTPResponse * res);
56- void handleURLParam (HTTPRequest * req, HTTPResponse * res);
56+ void handleQueryDemo (HTTPRequest * req, HTTPResponse * res);
57+ void handlePathParam (HTTPRequest * req, HTTPResponse * res);
5758void handle404 (HTTPRequest * req, HTTPResponse * res);
5859
5960void setup () {
@@ -72,27 +73,31 @@ void setup() {
7273
7374 // For every resource available on the server, we need to create a ResourceNode
7475 // The ResourceNode links URL and HTTP method to a handler function
75- ResourceNode * nodeRoot = new ResourceNode (" /" , " GET" , &handleRoot);
76- ResourceNode * nodeSVG = new ResourceNode (" /images/awesome.svg" , " GET" , &handleSVG);
77- ResourceNode * node404 = new ResourceNode (" " , " GET" , &handle404);
76+ ResourceNode * nodeRoot = new ResourceNode (" /" , " GET" , &handleRoot);
77+ ResourceNode * nodeSVG = new ResourceNode (" /images/awesome.svg" , " GET" , &handleSVG);
78+ ResourceNode * nodeQueryDemo = new ResourceNode (" /queryparams" , " GET" , &handleQueryDemo);
79+ ResourceNode * node404 = new ResourceNode (" " , " GET" , &handle404);
7880
79- // URL params
81+ // Path parameters
8082 // If you want (for example) to return a specific instance of an object type by its ID
8183 // you can use URLs like /led/1, led/2, ... - And you do not need to register one Resource
82- // Node per ID, but you can use wildcards in the URL definition. The following function
83- // has to wildcards, and will match for example to /urlparam/foo/bar, where "foo" and "bar"
84+ // Node per ID, but you can use wildcards in the route definition. The following route
85+ // has two wildcards, and will match for example to /urlparam/foo/bar, where "foo" and "bar"
8486 // are accessible parameters in the handler function.
8587 // Note: The wildcards can only be used between slashes at the moment (so /urlparam* would
8688 // not work).
87- ResourceNode * nodeURLParam = new ResourceNode (" /urlparam/*/*" , " GET" , &handleURLParam );
89+ ResourceNode * nodeURLParam = new ResourceNode (" /urlparam/*/*" , " GET" , &handlePathParam );
8890
8991 // Add the root node to the server
9092 secureServer.registerNode (nodeRoot);
9193
9294 // Add the SVG image
9395 secureServer.registerNode (nodeSVG);
9496
95- // Add the URL param node
97+ // Query parameter demo
98+ secureServer.registerNode (nodeQueryDemo);
99+
100+ // Add the path parameter
96101 // Note: The order of nodes may become important here. If you have one node for "/led" (e.g. list of LEDs)
97102 // and one node for /led/* (LED details), you should register the non-parameterized version first. The server
98103 // follows a first-match policy. If you would register the details node first, a call to /led/ will be targetted
@@ -126,9 +131,11 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) {
126131 res->println (" <!DOCTYPE html>" );
127132 res->println (" <html>" );
128133 res->println (" <head><title>Hello World!</title></head>" );
134+ res->println (" <style>.info{font-style:italic}</style>" );
129135 res->println (" <body>" );
130136
131- res->println (" <h1>GET parameters</h1>" );
137+ res->println (" <h1>Query Parameters</h1>" );
138+ res->println (" <p class=\" info\" >The parameters after the question mark in your URL.</p>" );
132139
133140 // Show a form to select a color to colorize the faces
134141 // We pass the selection as get parameter "shades" to this very same page,
@@ -158,8 +165,8 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) {
158165 // Depending on the selection we show the images in a specific color shade
159166 // Default is dark gray.
160167 int r = 63 , g = 63 , b = 63 ;
161- if (params-> isRequestParameterSet (paramName)) {
162- std::string paramVal = params->getRequestParameter (paramName);
168+ std::string paramVal;
169+ if ( params->getQueryParameter (paramName, paramVal)) {
163170 if (paramVal == " red" || paramVal == " magenta" || paramVal == " yellow" || paramVal == " rainbow" ) {
164171 r = 128 + random (0 , 128 );
165172 }
@@ -178,19 +185,21 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) {
178185
179186 res->print (" \" alt=\" Awesome!\" />" );
180187 }
188+ res->println (" <p>You'll find another demo <a href=\" /queryparams?a=42&b&c=13&a=hello\" >here</a>.</p>" );
181189
182- // Link to the URL parameter demo
183- res->println (" <h1>URL parameters</h1>" );
190+ // Link to the path parameter demo
191+ res->println (" <h1>Path Parameters</h1>" );
192+ res->println (" <p class=\" info\" >The parameters derived from placeholders in your path, like /foo/bar.</p>" );
184193 res->println (" <p>You'll find the demo <a href=\" /urlparam/foo/bar\" >here</a>.</p>" );
185194
186195 res->println (" </body>" );
187196 res->println (" </html>" );
188197}
189198
190- // This callback responds with an SVG image to a GET request. The icon is the awesome face.
199+ // This callback responds with an SVG image to a GET request. The icon is the " awesome face" .
191200// (borrowed from https://commons.wikimedia.org/wiki/File:718smiley.svg)
192201//
193- // If the color request parameter is set (so the URL is like awesome.svg?color=fede58), the
202+ // If the color query parameter is set (so the URL is like awesome.svg?color=fede58), the
194203// background of our awesome face is changed.
195204void handleSVG (HTTPRequest * req, HTTPResponse * res) {
196205 // Get access to the parameters
@@ -205,10 +214,11 @@ void handleSVG(HTTPRequest * req, HTTPResponse * res) {
205214 // Get request parameter (like awesome.svg?color=ff0000) and validate it
206215 std::string colorParamName = " color" ;
207216
208- // Check that the parameter is set
209- if (params->isRequestParameterSet (colorParamName)) {
210- // Get the value of the parameter
211- std::string requestColor = params->getRequestParameter (colorParamName);
217+ // Check that the parameter is set and retrieve it.
218+ // The getQueryParameter function will modify the second parameter, but only if the query
219+ // parameter is set.
220+ std::string requestColor;
221+ if (params->getQueryParameter (colorParamName, requestColor)) {
212222 // Check for correct length
213223 if (requestColor.length ()==6 ) {
214224 bool colorOk = true ;
@@ -247,10 +257,58 @@ void handleSVG(HTTPRequest * req, HTTPResponse * res) {
247257 res->print (" </svg>" );
248258}
249259
260+ // This is a more generic demo for the query parameters. It makes use of the iterator
261+ // interface to access them, which is useful if you do not know the paramter names in
262+ // adavance.
263+ void handleQueryDemo (HTTPRequest * req, HTTPResponse * res) {
264+ // A word of warning: In this example, we use the query parameters and directly print
265+ // them into the HTML output. We do this to simplify the demo. NEVER do this in a
266+ // real application, as it allows cross-site-scripting.
267+ res->setHeader (" Content-Type" , " text/html" );
268+
269+ res->println (" <!DOCTYPE html>" );
270+ res->println (" <html>" );
271+ res->println (" <head>" );
272+ res->println (" <title>Query Parameter Demo</title>" );
273+ res->println (" </head>" );
274+ res->println (" <body>" );
275+ res->println (" <p>The following query paramters have been set:</p>" );
276+
277+ // Start a table to display the parameters
278+ res->println (" <table style=\" border:1px solid black collapse;\" >" );
279+ res->println (" <tr><th>Key</th><th>Value</th></tr>" );
280+ // Iterate over the parameters. For more information, read about the C++ standard template library,
281+ // especially about vectors and iterators.
282+ ResourceParameters *params = req->getParams ();
283+ for (auto it = params->beginQueryParameters (); it != params->endQueryParameters (); ++it) {
284+ res->print (" <tr><td>" );
285+
286+ // The iterator yields std::pairs of std::strings. The first value contains the parameter key
287+ res->printStd ((*it).first );
288+ res->print (" </td><td>" );
289+
290+ // and the second value contains the parameter value
291+ res->printStd ((*it).second );
292+ res->println (" </td></tr>" );
293+ }
294+ res->println (" </table>" );
295+
296+ // You can retrieve the total parameter count from the parameters instance:
297+ res->print (" <p>There are a total of " );
298+ res->print (params->getQueryParameterCount ());
299+ res->print (" parameters, with " );
300+ res->print (params->getQueryParameterCount (true ));
301+ res->println (" unique keys.</p>" );
302+
303+ res->println (" <p>Go <a href=\" /\" >back to main page</a>.</p>" );
304+ res->println (" </body>" );
305+ res->println (" </html>" );
306+ }
307+
250308// This is a simple handler function that will show the content of URL parameters.
251309// If you call for example /urlparam/foo/bar, you will get the parameter values
252310// "foo" and "bar" provided by the ResourceParameters.
253- void handleURLParam (HTTPRequest * req, HTTPResponse * res) {
311+ void handlePathParam (HTTPRequest * req, HTTPResponse * res) {
254312 // Get access to the parameters
255313 ResourceParameters * params = req->getParams ();
256314
@@ -260,19 +318,26 @@ void handleURLParam(HTTPRequest * req, HTTPResponse * res) {
260318 // The url pattern is: /urlparam/*/*
261319 // This will make the content for the first parameter available on index 0,
262320 // and the second wildcard as index 1.
321+ // getPathParameter will - like getQueryParameter - write the value to the second parameter,
322+ // and return true, if the index is valid. Otherwise it returns false and leaves the second
323+ // parameter as it is.
263324
325+ std::string parameter1, parameter2;
264326 // Print the first parameter value
265- res->print (" Parameter 1: " );
266- res->printStd (params->getUrlParameter (0 ));
327+ if (params->getPathParameter (0 , parameter1)) {
328+ res->print (" Parameter 1: " );
329+ res->printStd (parameter1);
330+ }
331+
332+ res->println ();
267333
268334 // Print the second parameter value
269- res->print (" \n Parameter 2: " );
270- res->printStd (params->getUrlParameter (1 ));
335+ if (params->getPathParameter (1 , parameter2)) {
336+ res->print (" Parameter 2: " );
337+ res->printStd (parameter2);
338+ }
271339
272340 res->println (" \n\n Change the parameters in the URL to see how they get parsed!" );
273-
274- // Note: If you have objects that are identified by an ID, you may also use
275- // ResourceParameters::getUrlParameterInt(int) for convenience
276341}
277342
278343// For details to this function, see the Static-Page example
0 commit comments