comparison Core/HttpServer/MongooseServer.cpp @ 23:62bd05fe4b7c

support for ssl
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 Aug 2012 10:18:34 +0200
parents 27e2bbc11200
children 166664f0f860
comparison
equal deleted inserted replaced
22:1bc6327d1de3 23:62bd05fe4b7c
392 392
393 return PostDataStatus_Pending; 393 return PostDataStatus_Pending;
394 } 394 }
395 395
396 396
397 static bool Authorize(MongooseServer& that,
398 HttpOutput& output,
399 struct mg_connection *connection,
400 const struct mg_request_info *request)
401 {
402 /*std::string s = "HTTP/1.0 401 Unauthorized\r\n"
403 "WWW-Authenticate: Digest realm=\"www.palanthir.com\",qop=\"auth\",nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\""
404 "\r\n\r\n";
405 output.Send(&s[0], s.size());
406
407 return false;*/
408
409 return true;
410 }
411
412
397 413
398 static void* Callback(enum mg_event event, 414 static void* Callback(enum mg_event event,
399 struct mg_connection *connection, 415 struct mg_connection *connection,
400 const struct mg_request_info *request) 416 const struct mg_request_info *request)
401 { 417 {
409 for (int i = 0; i < request->num_headers; i++) 425 for (int i = 0; i < request->num_headers; i++)
410 { 426 {
411 std::string name = request->http_headers[i].name; 427 std::string name = request->http_headers[i].name;
412 std::transform(name.begin(), name.end(), name.begin(), ::tolower); 428 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
413 headers.insert(std::make_pair(name, request->http_headers[i].value)); 429 headers.insert(std::make_pair(name, request->http_headers[i].value));
430 }
431
432 printf("=========================\n");
433 printf(" URI: [%s]\n", request->uri);
434 for (HttpHandler::Arguments::const_iterator i = headers.begin(); i != headers.end(); i++)
435 {
436 printf("[%s] = [%s]\n", i->first.c_str(), i->second.c_str());
437 }
438
439 // Authenticate this connection
440 if (!Authorize(*that, c, connection, request))
441 {
442 return (void*) "";
414 } 443 }
415 444
416 std::string postData; 445 std::string postData;
417 446
418 if (!strcmp(request->request_method, "GET")) 447 if (!strcmp(request->request_method, "GET"))
499 528
500 529
501 MongooseServer::MongooseServer() : pimpl_(new PImpl) 530 MongooseServer::MongooseServer() : pimpl_(new PImpl)
502 { 531 {
503 pimpl_->context_ = NULL; 532 pimpl_->context_ = NULL;
533 ssl_ = false;
504 port_ = 8000; 534 port_ = 8000;
505 } 535 }
506 536
507 537
508 MongooseServer::~MongooseServer() 538 MongooseServer::~MongooseServer()
522 { 552 {
523 if (!IsRunning()) 553 if (!IsRunning())
524 { 554 {
525 std::string port = boost::lexical_cast<std::string>(port_); 555 std::string port = boost::lexical_cast<std::string>(port_);
526 556
557 if (ssl_)
558 {
559 port += "s";
560 }
561
527 const char *options[] = { 562 const char *options[] = {
528 "listening_ports", port.c_str(), 563 "listening_ports", port.c_str(),
564 ssl_ ? "ssl_certificate" : NULL,
565 certificate_.c_str(),
529 NULL 566 NULL
530 }; 567 };
531 568
532 pimpl_->context_ = mg_start(&Callback, this, options); 569 pimpl_->context_ = mg_start(&Callback, this, options);
533 if (!pimpl_->context_) 570 if (!pimpl_->context_)
564 { 601 {
565 delete *it; 602 delete *it;
566 } 603 }
567 } 604 }
568 605
606
607 void MongooseServer::RegisterUser(const char* username,
608 const char* password)
609 {
610 Stop();
611 registeredUsers_[username] = password;
612 }
613
614 void MongooseServer::SetSslEnabled(bool enabled)
615 {
616 Stop();
617
618 #if PALANTIR_SSL_ENABLED == 0
619 if (enabled)
620 {
621 throw PalantirException("Palantir has been build without SSL support");
622 }
623 else
624 {
625 ssl_ = false;
626 }
627 #else
628 ssl_ = enabled;
629 #endif
630 }
631
632 void MongooseServer::SetSslCertificate(const char* path)
633 {
634 Stop();
635 certificate_ = path;
636 }
569 } 637 }