@@ -174,6 +174,60 @@ func (r *HTTPRequest) CurlCommand(scheme string, backend *Backend) string {
174174 return s .String ()
175175}
176176
177+ // HurlFile generates a new hurl file as a string
178+ //
179+ // scheme can be "auto", "http://" or "https://"
180+ func (r * HTTPRequest ) HurlFile (scheme string , backend * Backend ) string {
181+ var s strings.Builder
182+
183+ // Parse scheme
184+ switch scheme {
185+ case "auto" :
186+ if r .port == "443" {
187+ scheme = "https://"
188+ } else {
189+ // default to http for 80, empty, or any other port
190+ scheme = "http://"
191+ }
192+ case "http://" , "https://" :
193+ // keep as-is
194+ default :
195+ return "invalid scheme: " + scheme
196+ }
197+
198+ // Build host URL, append port only when provided
199+ hostURL := r .host
200+ if r .port != "" {
201+ hostURL = net .JoinHostPort (r .host , r .port )
202+ }
203+
204+ // Start hurl file
205+ s .WriteString (fmt .Sprintf ("%s %s%s%s\n " , r .method , scheme , hostURL , r .url ))
206+
207+ // Headers
208+ for _ , h := range r .headers {
209+ if h .name == vsl .HdrNameHost {
210+ continue
211+ }
212+ s .WriteString (fmt .Sprintf ("%s: %s\n " , h .name , h .value ))
213+ }
214+
215+ // Options
216+ s .WriteString ("\n [Options]\n insecure: true\n " )
217+
218+ // Connect-to
219+ // --connect-to HOST1:PORT1:HOST2:PORT2
220+ // when you would connect to HOST1:PORT1, actually connect to HOST2:PORT2
221+ if backend != nil {
222+ s .WriteString ("\n # To connect to the backend run the hurl file as:\n " )
223+ s .WriteString (fmt .Sprintf (`# hurl --connect-to "%s:%s:%s" file.hurl` ,
224+ escapeDoubleQuotes (hostURL ), escapeDoubleQuotes (backend .host ), backend .port ,
225+ ))
226+ }
227+
228+ return s .String ()
229+ }
230+
177231// escapeDoubleQuotes is an utility function to escape double quotes ;)
178232func escapeDoubleQuotes (s string ) string {
179233 return strings .ReplaceAll (s , `"` , `\"` )
0 commit comments