|
46 | 46 | LastHttpContent) |
47 | 47 | (io.netty.handler.ssl |
48 | 48 | ApplicationProtocolNames |
49 | | - SslContext) |
| 49 | + SslContext |
| 50 | + SslHandler) |
50 | 51 | (io.netty.handler.stream |
51 | 52 | ChunkedWriteHandler) |
52 | 53 | (io.netty.util AsciiString) |
|
633 | 634 |
|
634 | 635 | (defn make-pipeline-builder |
635 | 636 | "Returns a function that initializes a new server channel's pipeline." |
636 | | - [handler {:keys [ssl? ^SslContext ssl-context use-h2c?] :as opts}] |
| 637 | + [handler {:keys [ssl? |
| 638 | + ^SslContext ssl-context |
| 639 | + use-h2c? |
| 640 | + initial-pipeline-transform] |
| 641 | + :or {initial-pipeline-transform identity} |
| 642 | + :as opts}] |
637 | 643 | (fn pipeline-builder* |
638 | 644 | [^ChannelPipeline pipeline] |
639 | 645 | (log/trace "pipeline-builder*" pipeline opts) |
640 | 646 | (let [setup-opts (assoc opts |
641 | 647 | :handler handler |
642 | 648 | :server? true |
643 | 649 | :pipeline pipeline)] |
644 | | - (cond (and ssl? ssl-context) |
645 | | - (let [ssl-handler (netty/ssl-handler (.channel pipeline) ssl-context)] |
646 | | - (log/debug "Setting up secure HTTP server pipeline.") |
647 | | - (log/debug "ALPN HTTP versions:" (mapv str (.nextProtocols ssl-context))) |
648 | | - |
649 | | - (-> pipeline |
650 | | - (.addLast "ssl-handler" ssl-handler) |
651 | | - (.addLast "apn-handler" |
652 | | - (ApnHandler. |
653 | | - (fn setup-secure-pipeline |
654 | | - [^ChannelPipeline pipeline protocol] |
655 | | - (log/trace "setup-secure-pipeline: chosen protocol:" protocol) |
656 | | - (when (nil? (.applicationProtocol ssl-handler)) |
657 | | - (log/debug (str "ALPN not used. Protocol " protocol " chosen by fallback."))) |
658 | | - (cond (.equals ApplicationProtocolNames/HTTP_1_1 protocol) |
659 | | - (setup-http1-pipeline setup-opts) |
660 | | - |
661 | | - (.equals ApplicationProtocolNames/HTTP_2 protocol) |
662 | | - (http2/setup-conn-pipeline setup-opts) |
663 | | - |
664 | | - :else |
665 | | - (let [msg (str "Unknown protocol: " protocol) |
666 | | - e (IllegalStateException. msg)] |
667 | | - (log/error e msg) |
668 | | - (throw e)))) |
669 | | - apn-fallback-protocol))) |
| 650 | + (initial-pipeline-transform pipeline) |
| 651 | + (cond ssl? |
| 652 | + (do |
| 653 | + ;; might be nil in manual-ssl? mode |
| 654 | + (when ssl-context |
| 655 | + (log/debug "Setting up secure HTTP server pipeline.") |
| 656 | + (log/debug "ALPN HTTP versions:" (mapv str (.nextProtocols ssl-context))) |
| 657 | + (.addLast pipeline "ssl-handler" (netty/ssl-handler (.channel pipeline) ssl-context))) |
| 658 | + (.addLast pipeline |
| 659 | + "apn-handler" |
| 660 | + (ApnHandler. |
| 661 | + (fn setup-secure-pipeline |
| 662 | + [^ChannelPipeline pipeline protocol] |
| 663 | + (log/trace "setup-secure-pipeline: chosen protocol:" protocol) |
| 664 | + (let [^SslHandler ssl-handler (.get pipeline SslHandler)] |
| 665 | + (when (nil? (.applicationProtocol ssl-handler)) |
| 666 | + (log/debug (str "ALPN not used. Protocol " protocol " chosen by fallback."))) |
| 667 | + (cond (.equals ApplicationProtocolNames/HTTP_1_1 protocol) |
| 668 | + (setup-http1-pipeline setup-opts) |
| 669 | + |
| 670 | + (.equals ApplicationProtocolNames/HTTP_2 protocol) |
| 671 | + (http2/setup-conn-pipeline setup-opts) |
| 672 | + |
| 673 | + :else |
| 674 | + (let [msg (str "Unknown protocol: " protocol) |
| 675 | + e (IllegalStateException. msg)] |
| 676 | + (log/error e msg) |
| 677 | + (throw e))))) |
| 678 | + apn-fallback-protocol)) |
670 | 679 | pipeline) |
671 | 680 |
|
672 | 681 | use-h2c? |
|
750 | 759 | opts (assoc opts :ssl-context ssl-context) |
751 | 760 | http1-pipeline-transform (common/validate-http1-pipeline-transform opts) |
752 | 761 | executor (setup-executor executor) |
753 | | - continue-executor (setup-continue-executor executor continue-executor) |
754 | | - pipeline-builder (make-pipeline-builder |
755 | | - handler |
756 | | - (assoc opts |
757 | | - :executor executor |
758 | | - :ssl? (or manual-ssl? (boolean ssl-context)) |
759 | | - :http1-pipeline-transform http1-pipeline-transform |
760 | | - :continue-executor continue-executor))] |
| 762 | + continue-executor (setup-continue-executor executor continue-executor)] |
761 | 763 |
|
762 | 764 | (if (some #{:http2} http-versions) |
763 | 765 | (when (and (not ssl-context) |
764 | | - (not use-h2c?)) |
765 | | - (throw (IllegalArgumentException. "HTTP/2 requires ssl-context to be given or use-h2c? to be true."))) |
| 766 | + (not use-h2c?) |
| 767 | + (not manual-ssl?)) |
| 768 | + (throw (IllegalArgumentException. "HTTP/2 requires passing an ssl-context or manual-ssl? true. Alternatively, pass use-h2c? true to disable TLS."))) |
766 | 769 | (when use-h2c? |
767 | 770 | (throw (IllegalArgumentException. "use-h2c? may only be true when HTTP/2 is enabled.")))) |
768 | 771 |
|
769 | 772 | (when (and ssl-context |
770 | 773 | use-h2c?) |
771 | 774 | (throw (IllegalArgumentException. "use-h2c? must not be true when ssl-context is given."))) |
772 | 775 |
|
| 776 | + (when (and ssl-context |
| 777 | + manual-ssl?) |
| 778 | + (throw (IllegalArgumentException. "manual-ssl? must not be true when ssl-context is given."))) |
| 779 | + |
773 | 780 | (netty/start-server |
774 | | - {:pipeline-builder pipeline-builder |
| 781 | + {:pipeline-builder (make-pipeline-builder |
| 782 | + handler |
| 783 | + (assoc opts |
| 784 | + :executor executor |
| 785 | + :ssl? (or manual-ssl? (boolean ssl-context)) |
| 786 | + :http1-pipeline-transform http1-pipeline-transform |
| 787 | + :continue-executor continue-executor)) |
775 | 788 | :bootstrap-transform bootstrap-transform |
776 | 789 | :socket-address (if socket-address |
777 | 790 | socket-address |
|
0 commit comments