Skip to content

Instantly share code, notes, and snippets.

@tclamb
Created December 29, 2013 16:41
Show Gist options
  • Save tclamb/8172136 to your computer and use it in GitHub Desktop.
Save tclamb/8172136 to your computer and use it in GitHub Desktop.
modification of `webserver.cpp` example that shows how to use `url_obj`
#include <iostream>
#include <native/native.h>
#include <sstream>
using namespace native::http;
int main() {
http server;
int port = 8080;
if(!server.listen("0.0.0.0", port, [](request& req, response& res) {
res.set_status(200);
res.set_header("Content-Type", "text/plain");
auto url = req.url();
std::stringstream ss;
ss << " req.url().schema(): " << url.schema() << '\n'
<< " req.url().host(): " << url.host() << '\n'
<< " req.url().port(): " << url.port() << '\n'
<< " req.url().path(): " << url.path() << '\n'
<< " req.url().query(): " << url.query() << '\n'
<< "req.url().fragment(): " << url.fragment() << "\n\n"
<< "Note that port() == 0 and fragment() == \"\"\n";
if(url.path() == "/foo") {
ss << "\nC++ FTW\n";
}
res.end(ss.str());
})) return 1;
std::cout << "Server running at http://0.0.0.0:" << port << "/" << std::endl;
return native::run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment