@@ -50,7 +50,8 @@ import {
50
50
WebSocketPassThroughHandler ,
51
51
EchoWebSocketHandlerDefinition ,
52
52
RejectWebSocketHandlerDefinition ,
53
- ListenWebSocketHandlerDefinition
53
+ ListenWebSocketHandlerDefinition ,
54
+ WebSocketForwardToHostHandler
54
55
} from '../../model/rules/definitions/websocket-rule-definitions' ;
55
56
import {
56
57
EthereumCallResultHandler ,
@@ -151,7 +152,11 @@ export function HandlerConfiguration(props: {
151
152
case 'file' :
152
153
return < FromFileResponseHandlerConfig { ...configProps } /> ;
153
154
case 'forward-to-host' :
154
- return < ForwardToHostHandlerConfig { ...configProps } /> ;
155
+ case 'ws-forward-to-host' :
156
+ return < ForwardToHostHandlerConfig
157
+ { ...configProps }
158
+ handlerKey = { handlerKey }
159
+ /> ;
155
160
case 'passthrough' :
156
161
case 'ws-passthrough' :
157
162
return < PassThroughHandlerConfig { ...configProps } /> ;
@@ -657,9 +662,14 @@ const UrlInput = styled(TextInput)`
657
662
658
663
@inject ( 'rulesStore' )
659
664
@observer
660
- class ForwardToHostHandlerConfig extends HandlerConfig < ForwardToHostHandler , {
661
- rulesStore ?: RulesStore
662
- } > {
665
+ class ForwardToHostHandlerConfig extends HandlerConfig <
666
+ | ForwardToHostHandler
667
+ | WebSocketForwardToHostHandler ,
668
+ {
669
+ rulesStore ?: RulesStore ,
670
+ handlerKey : 'forward-to-host' | 'ws-forward-to-host'
671
+ }
672
+ > {
663
673
664
674
@observable
665
675
private error : Error | undefined ;
@@ -682,9 +692,19 @@ class ForwardToHostHandlerConfig extends HandlerConfig<ForwardToHostHandler, {
682
692
}
683
693
684
694
render ( ) {
685
- const { targetHost, updateHostHeader, error, onTargetChange, onUpdateHeaderChange } = this ;
695
+ const {
696
+ targetHost,
697
+ updateHostHeader,
698
+ error,
699
+ onTargetChange,
700
+ onUpdateHeaderChange
701
+ } = this ;
686
702
const { targetHost : savedTargetHost } = this . props . handler . forwarding ! ;
687
703
704
+ const messageType = this . props . handlerKey === 'ws-forward-to-host'
705
+ ? 'WebSocket'
706
+ : 'request' ;
707
+
688
708
return < ConfigContainer >
689
709
< SectionLabel > Replacement host</ SectionLabel >
690
710
< UrlInput
@@ -699,7 +719,7 @@ class ForwardToHostHandlerConfig extends HandlerConfig<ForwardToHostHandler, {
699
719
value = { updateHostHeader . toString ( ) }
700
720
onChange = { onUpdateHeaderChange }
701
721
title = { dedent `
702
- Most servers will not accept requests that arrive
722
+ Most servers will not accept ${ messageType } s that arrive
703
723
with the wrong host header, so it's typically useful
704
724
to automatically change it to match the new host
705
725
` }
@@ -709,7 +729,7 @@ class ForwardToHostHandlerConfig extends HandlerConfig<ForwardToHostHandler, {
709
729
</ ConfigSelect >
710
730
{ savedTargetHost &&
711
731
< ConfigExplanation >
712
- All matching requests will be forwarded to { savedTargetHost } ,
732
+ All matching { messageType } s will be forwarded to { savedTargetHost } ,
713
733
keeping their existing path{
714
734
! savedTargetHost . includes ( '://' ) ? ', protocol,' : ''
715
735
} and query string.{
@@ -726,26 +746,47 @@ class ForwardToHostHandlerConfig extends HandlerConfig<ForwardToHostHandler, {
726
746
try {
727
747
if ( ! this . targetHost ) throw new Error ( 'A target host is required' ) ;
728
748
729
- const protocolMatch = this . targetHost . match ( / ^ \w + : \/ \/ / ) ;
730
- if ( protocolMatch ) {
731
- const pathWithoutProtocol = this . targetHost . slice ( protocolMatch [ 0 ] . length ) ;
749
+ let urlWithoutProtocol : string ;
732
750
733
- if ( pathWithoutProtocol . includes ( '/' ) ) {
734
- throw new Error ( 'The replacement host shouldn\'t include a path, since it won\'t be used' ) ;
735
- }
736
- if ( pathWithoutProtocol . includes ( '?' ) ) {
737
- throw new Error ( 'The replacement host shouldn\'t include a query string, since it won\'t be used' ) ;
751
+ const protocolMatch = this . targetHost . match ( / ^ ( \w + ) : \/ \/ / ) ;
752
+ if ( protocolMatch ) {
753
+ const validProtocols = this . props . handlerKey === 'ws-forward-to-host'
754
+ ? [ 'ws' , 'wss' ]
755
+ : [ 'http' , 'https' ] ;
756
+
757
+ if ( ! validProtocols . includes ( protocolMatch [ 1 ] . toLowerCase ( ) ) ) {
758
+ throw new Error (
759
+ `The protocol must be either ${ validProtocols [ 0 ] } or ${ validProtocols [ 1 ] } `
760
+ ) ;
738
761
}
762
+
763
+ urlWithoutProtocol = this . targetHost . slice ( protocolMatch [ 0 ] . length ) ;
739
764
} else {
740
- if ( this . targetHost . includes ( '/' ) ) {
741
- throw new Error ( 'The replacement host shouldn\'t include a path, since it won\'t be used' ) ;
742
- }
743
- if ( this . targetHost . includes ( '?' ) ) {
744
- throw new Error ( 'The replacement host shouldn\'t include a query string, since it won\'t be used' ) ;
745
- }
765
+ urlWithoutProtocol = this . targetHost ;
766
+ }
767
+
768
+ if ( urlWithoutProtocol . includes ( '/' ) ) {
769
+ throw new Error (
770
+ 'The replacement host shouldn\'t include a path, since it won\'t be used'
771
+ ) ;
772
+ }
773
+ if ( urlWithoutProtocol . includes ( '?' ) ) {
774
+ throw new Error (
775
+ 'The replacement host shouldn\'t include a query string, since it won\'t be used'
776
+ ) ;
746
777
}
747
778
748
- this . props . onChange ( new ForwardToHostHandler ( this . targetHost , this . updateHostHeader , this . props . rulesStore ! ) ) ;
779
+ const HandlerClass = this . props . handlerKey === 'ws-forward-to-host'
780
+ ? WebSocketForwardToHostHandler
781
+ : ForwardToHostHandler ;
782
+
783
+ this . props . onChange (
784
+ new HandlerClass (
785
+ this . targetHost ,
786
+ this . updateHostHeader ,
787
+ this . props . rulesStore !
788
+ )
789
+ ) ;
749
790
this . error = undefined ;
750
791
} catch ( e ) {
751
792
console . log ( e ) ;
0 commit comments