Mercurial > hg > orthanc
comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3416:541c787e2230
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 11 Jun 2019 21:38:34 +0200 |
parents | b9cba6a91780 |
children | e2b032584a83 |
comparison
equal
deleted
inserted
replaced
3415:2a821deece64 | 3416:541c787e2230 |
---|---|
2602 } | 2602 } |
2603 #endif | 2603 #endif |
2604 } | 2604 } |
2605 | 2605 |
2606 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ | 2606 #endif /* HAS_ORTHANC_PLUGIN_HTTP_CLIENT == 1 */ |
2607 | |
2608 | |
2609 | |
2610 | |
2611 | |
2612 /****************************************************************** | |
2613 ** CHUNKED HTTP SERVER | |
2614 ******************************************************************/ | |
2615 | |
2616 namespace Internals | |
2617 { | |
2618 void NullRestCallback(OrthancPluginRestOutput* output, | |
2619 const char* url, | |
2620 const OrthancPluginHttpRequest* request) | |
2621 { | |
2622 } | |
2623 | |
2624 IChunkedRequestReader *NullChunkedRestCallback(const char* url, | |
2625 const OrthancPluginHttpRequest* request) | |
2626 { | |
2627 return NULL; | |
2628 } | |
2629 | |
2630 | |
2631 #if HAS_ORTHANC_PLUGIN_CHUNKED_HTTP_SERVER == 1 | |
2632 | |
2633 OrthancPluginErrorCode ChunkedRequestReaderAddChunk( | |
2634 OrthancPluginServerChunkedRequestReader* reader, | |
2635 const void* data, | |
2636 uint32_t size) | |
2637 { | |
2638 try | |
2639 { | |
2640 if (reader == NULL) | |
2641 { | |
2642 return OrthancPluginErrorCode_InternalError; | |
2643 } | |
2644 | |
2645 reinterpret_cast<IChunkedRequestReader*>(reader)->AddChunk(data, size); | |
2646 return OrthancPluginErrorCode_Success; | |
2647 } | |
2648 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2649 { | |
2650 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2651 } | |
2652 catch (boost::bad_lexical_cast&) | |
2653 { | |
2654 return OrthancPluginErrorCode_BadFileFormat; | |
2655 } | |
2656 catch (...) | |
2657 { | |
2658 return OrthancPluginErrorCode_Plugin; | |
2659 } | |
2660 } | |
2661 | |
2662 | |
2663 OrthancPluginErrorCode ChunkedRequestReaderExecute( | |
2664 OrthancPluginServerChunkedRequestReader* reader, | |
2665 OrthancPluginRestOutput* output) | |
2666 { | |
2667 try | |
2668 { | |
2669 if (reader == NULL) | |
2670 { | |
2671 return OrthancPluginErrorCode_InternalError; | |
2672 } | |
2673 | |
2674 reinterpret_cast<IChunkedRequestReader*>(reader)->Execute(output); | |
2675 return OrthancPluginErrorCode_Success; | |
2676 } | |
2677 catch (ORTHANC_PLUGINS_EXCEPTION_CLASS& e) | |
2678 { | |
2679 return static_cast<OrthancPluginErrorCode>(e.GetErrorCode()); | |
2680 } | |
2681 catch (boost::bad_lexical_cast&) | |
2682 { | |
2683 return OrthancPluginErrorCode_BadFileFormat; | |
2684 } | |
2685 catch (...) | |
2686 { | |
2687 return OrthancPluginErrorCode_Plugin; | |
2688 } | |
2689 } | |
2690 | |
2691 | |
2692 void ChunkedRequestReaderFinalize( | |
2693 OrthancPluginServerChunkedRequestReader* reader) | |
2694 { | |
2695 if (reader != NULL) | |
2696 { | |
2697 delete reinterpret_cast<IChunkedRequestReader*>(reader); | |
2698 } | |
2699 } | |
2700 | |
2701 #else | |
2702 | |
2703 void ChunkedRestCompatibility(OrthancPluginRestOutput* output, | |
2704 const char* url, | |
2705 const OrthancPluginHttpRequest* request, | |
2706 RestCallback GetHandler, | |
2707 ChunkedRestCallback PostHandler, | |
2708 RestCallback DeleteHandler, | |
2709 ChunkedRestCallback PutHandler) | |
2710 { | |
2711 std::string allowed; | |
2712 | |
2713 if (GetHandler != Internals::NullRestCallback) | |
2714 { | |
2715 allowed += "GET"; | |
2716 } | |
2717 | |
2718 if (PostHandler != Internals::NullChunkedRestCallback) | |
2719 { | |
2720 if (!allowed.empty()) | |
2721 { | |
2722 allowed += ","; | |
2723 } | |
2724 | |
2725 allowed += "POST"; | |
2726 } | |
2727 | |
2728 if (DeleteHandler != Internals::NullRestCallback) | |
2729 { | |
2730 if (!allowed.empty()) | |
2731 { | |
2732 allowed += ","; | |
2733 } | |
2734 | |
2735 allowed += "DELETE"; | |
2736 } | |
2737 | |
2738 if (PutHandler != Internals::NullChunkedRestCallback) | |
2739 { | |
2740 if (!allowed.empty()) | |
2741 { | |
2742 allowed += ","; | |
2743 } | |
2744 | |
2745 allowed += "PUT"; | |
2746 } | |
2747 | |
2748 switch (request->method) | |
2749 { | |
2750 case OrthancPluginHttpMethod_Get: | |
2751 { | |
2752 if (GetHandler == Internals::NullRestCallback) | |
2753 { | |
2754 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
2755 } | |
2756 else | |
2757 { | |
2758 GetHandler(output, url, request); | |
2759 } | |
2760 return; | |
2761 } | |
2762 | |
2763 case OrthancPluginHttpMethod_Post: | |
2764 { | |
2765 if (PostHandler == Internals::NullChunkedRestCallback) | |
2766 { | |
2767 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
2768 } | |
2769 else | |
2770 { | |
2771 std::auto_ptr<IChunkedRequestReader> reader(PostHandler(url, request)); | |
2772 if (reader.get() == NULL) | |
2773 { | |
2774 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
2775 } | |
2776 else | |
2777 { | |
2778 reader->AddChunk(request->body, request->bodySize); | |
2779 reader->Execute(output); | |
2780 } | |
2781 } | |
2782 return; | |
2783 } | |
2784 | |
2785 case OrthancPluginHttpMethod_Delete: | |
2786 { | |
2787 if (DeleteHandler == Internals::NullRestCallback) | |
2788 { | |
2789 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
2790 } | |
2791 else | |
2792 { | |
2793 DeleteHandler(output, url, request); | |
2794 } | |
2795 return; | |
2796 } | |
2797 | |
2798 case OrthancPluginHttpMethod_Put: | |
2799 { | |
2800 if (PutHandler == Internals::NullChunkedRestCallback) | |
2801 { | |
2802 OrthancPluginSendMethodNotAllowed(GetGlobalContext(), output, allowed.c_str()); | |
2803 } | |
2804 else | |
2805 { | |
2806 std::auto_ptr<IChunkedRequestReader> reader(PutHandler(url, request)); | |
2807 if (reader.get() == NULL) | |
2808 { | |
2809 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); | |
2810 } | |
2811 else | |
2812 { | |
2813 reader->AddChunk(request->body, request->bodySize); | |
2814 reader->Execute(output); | |
2815 } | |
2816 } | |
2817 return; | |
2818 } | |
2819 | |
2820 default: | |
2821 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
2822 } | |
2823 } | |
2824 #endif | |
2825 } | |
2607 } | 2826 } |