comparison Plugins/Engine/OrthancPlugins.cpp @ 3415:2a821deece64

refactoring to handle "not allowed" HTTP status 405
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 11 Jun 2019 21:06:57 +0200
parents b9cba6a91780
children 0a0e7eca95ae
comparison
equal deleted inserted replaced
3414:b9cba6a91780 3415:2a821deece64
552 552
553 class ChunkedRestCallback : public boost::noncopyable 553 class ChunkedRestCallback : public boost::noncopyable
554 { 554 {
555 private: 555 private:
556 _OrthancPluginChunkedRestCallback parameters_; 556 _OrthancPluginChunkedRestCallback parameters_;
557 boost::regex regex_; 557 boost::regex regex_;
558 558
559 public: 559 public:
560 ChunkedRestCallback(_OrthancPluginChunkedRestCallback parameters) : 560 ChunkedRestCallback(_OrthancPluginChunkedRestCallback parameters) :
561 parameters_(parameters), 561 parameters_(parameters),
562 regex_(parameters.pathRegularExpression) 562 regex_(parameters.pathRegularExpression)
1372 } 1372 }
1373 }; 1373 };
1374 } 1374 }
1375 1375
1376 1376
1377 static std::string GetAllowedMethods(_OrthancPluginChunkedRestCallback parameters)
1378 {
1379 std::string s;
1380
1381 if (parameters.getHandler != NULL)
1382 {
1383 s += "GET";
1384 }
1385
1386 if (parameters.postHandler != NULL)
1387 {
1388 if (!s.empty())
1389 {
1390 s+= ",";
1391 }
1392
1393 s += "POST";
1394 }
1395
1396 if (parameters.deleteHandler != NULL)
1397 {
1398 if (!s.empty())
1399 {
1400 s+= ",";
1401 }
1402
1403 s += "DELETE";
1404 }
1405
1406 if (parameters.putHandler != NULL)
1407 {
1408 if (!s.empty())
1409 {
1410 s+= ",";
1411 }
1412
1413 s += "PUT";
1414 }
1415
1416 return s;
1417 }
1418
1419
1377 bool OrthancPlugins::HandleChunkedGetDelete(HttpOutput& output, 1420 bool OrthancPlugins::HandleChunkedGetDelete(HttpOutput& output,
1378 HttpMethod method, 1421 HttpMethod method,
1379 const UriComponents& uri, 1422 const UriComponents& uri,
1380 const Arguments& headers, 1423 const Arguments& headers,
1381 const GetArguments& getArguments) 1424 const GetArguments& getArguments)
1382 { 1425 {
1383 if (method == HttpMethod_Get || 1426 RestCallbackMatcher matcher(uri);
1384 method == HttpMethod_Delete) 1427
1385 { 1428 PImpl::ChunkedRestCallback* callback = NULL;
1386 RestCallbackMatcher matcher(uri); 1429
1387 1430 // Loop over the callbacks registered by the plugins
1388 PImpl::ChunkedRestCallback* callback = NULL; 1431 for (PImpl::ChunkedRestCallbacks::const_iterator it = pimpl_->chunkedRestCallbacks_.begin();
1389 1432 it != pimpl_->chunkedRestCallbacks_.end(); ++it)
1390 // Loop over the callbacks registered by the plugins 1433 {
1391 for (PImpl::ChunkedRestCallbacks::const_iterator it = pimpl_->chunkedRestCallbacks_.begin(); 1434 if (matcher.IsMatch((*it)->GetRegularExpression()))
1392 it != pimpl_->chunkedRestCallbacks_.end(); ++it) 1435 {
1393 { 1436 callback = *it;
1394 if (matcher.IsMatch((*it)->GetRegularExpression())) 1437 break;
1395 { 1438 }
1396 callback = *it; 1439 }
1440
1441 if (callback == NULL)
1442 {
1443 return false;
1444 }
1445 else
1446 {
1447 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << matcher.GetFlatUri();
1448
1449 OrthancPluginRestCallback handler;
1450
1451 switch (method)
1452 {
1453 case HttpMethod_Get:
1454 handler = callback->GetParameters().getHandler;
1397 break; 1455 break;
1398 } 1456
1399 } 1457 case HttpMethod_Delete:
1400 1458 handler = callback->GetParameters().deleteHandler;
1401 if (callback != NULL) 1459 break;
1402 { 1460
1403 LOG(INFO) << "Delegating HTTP request to plugin for URI: " << matcher.GetFlatUri(); 1461 default:
1404 1462 handler = NULL;
1463 break;
1464 }
1465
1466 if (handler == NULL)
1467 {
1468 output.SendMethodNotAllowed(GetAllowedMethods(callback->GetParameters()));
1469 }
1470 else
1471 {
1405 HttpRequestConverter converter(matcher, method, headers); 1472 HttpRequestConverter converter(matcher, method, headers);
1406 converter.SetGetArguments(getArguments); 1473 converter.SetGetArguments(getArguments);
1407 1474
1408 PImpl::PluginHttpOutput pluginOutput(output); 1475 PImpl::PluginHttpOutput pluginOutput(output);
1409 1476
1410 assert(callback != NULL); 1477 OrthancPluginErrorCode error = handler(
1411 OrthancPluginErrorCode error = callback->GetParameters().handler 1478 reinterpret_cast<OrthancPluginRestOutput*>(&pluginOutput),
1412 (reinterpret_cast<OrthancPluginRestOutput*>(&pluginOutput), 1479 matcher.GetFlatUri().c_str(), &converter.GetRequest());
1413 NULL /* no reader */, matcher.GetFlatUri().c_str(), &converter.GetRequest());
1414 1480
1415 pluginOutput.Close(error, GetErrorDictionary()); 1481 pluginOutput.Close(error, GetErrorDictionary());
1416 return true; 1482 }
1417 } 1483
1418 } 1484 return true;
1419 1485 }
1420 return false;
1421 } 1486 }
1422 1487
1423 1488
1424 bool OrthancPlugins::Handle(HttpOutput& output, 1489 bool OrthancPlugins::Handle(HttpOutput& output,
1425 RequestOrigin /*origin*/, 1490 RequestOrigin /*origin*/,
4283 if (callback == NULL) 4348 if (callback == NULL)
4284 { 4349 {
4285 // Callback not found 4350 // Callback not found
4286 return false; 4351 return false;
4287 } 4352 }
4288 4353 else
4289 LOG(INFO) << "Delegating chunked HTTP request to plugin for URI: " << matcher.GetFlatUri(); 4354 {
4290 4355 OrthancPluginServerChunkedRequestReaderFactory handler;
4291 HttpRequestConverter converter(matcher, method, headers); 4356
4292 converter.GetRequest().body = NULL; 4357 switch (method)
4293 converter.GetRequest().bodySize = 0; 4358 {
4294 4359 case HttpMethod_Post:
4295 OrthancPluginServerChunkedRequestReader* reader = NULL; 4360 handler = callback->GetParameters().postHandler;
4361 break;
4362
4363 case HttpMethod_Put:
4364 handler = callback->GetParameters().putHandler;
4365 break;
4366
4367 default:
4368 handler = NULL;
4369 break;
4370 }
4371
4372 if (handler == NULL)
4373 {
4374 return false;
4375 }
4376 else
4377 {
4378 LOG(INFO) << "Delegating chunked HTTP request to plugin for URI: " << matcher.GetFlatUri();
4379
4380 HttpRequestConverter converter(matcher, method, headers);
4381 converter.GetRequest().body = NULL;
4382 converter.GetRequest().bodySize = 0;
4383
4384 OrthancPluginServerChunkedRequestReader* reader = NULL;
4296 4385
4297 OrthancPluginErrorCode errorCode = callback->GetParameters().handler( 4386 OrthancPluginErrorCode errorCode = handler(
4298 NULL /* no HTTP output */, &reader, matcher.GetFlatUri().c_str(), &converter.GetRequest()); 4387 &reader, matcher.GetFlatUri().c_str(), &converter.GetRequest());
4299 4388
4300 if (reader == NULL) 4389 if (reader == NULL)
4301 { 4390 {
4302 // The plugin has not created a reader for chunked body 4391 // The plugin has not created a reader for chunked body
4303 return false; 4392 return false;
4304 } 4393 }
4305 else if (errorCode != OrthancPluginErrorCode_Success) 4394 else if (errorCode != OrthancPluginErrorCode_Success)
4306 { 4395 {
4307 throw OrthancException(static_cast<ErrorCode>(errorCode)); 4396 throw OrthancException(static_cast<ErrorCode>(errorCode));
4308 } 4397 }
4309 else 4398 else
4310 { 4399 {
4311 target.reset(new HttpServerChunkedReader(reader, callback->GetParameters(), GetErrorDictionary())); 4400 target.reset(new HttpServerChunkedReader(reader, callback->GetParameters(), GetErrorDictionary()));
4312 return true; 4401 return true;
4402 }
4403 }
4313 } 4404 }
4314 } 4405 }
4315 } 4406 }