Skip to content

Commit 2985d3f

Browse files
committed
Support rewriting the request_url in the HttpClient when no Host header is provided
1 parent 5a5975c commit 2985d3f

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

lib/shopify_api/clients/http_client.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ def request(request, response_as_struct: false)
3939
headers = @headers
4040
headers["Content-Type"] = T.must(request.body_type) if request.body_type
4141
headers = headers.merge(T.must(request.extra_headers)) if request.extra_headers
42-
if headers["Host"].include?(".my.shop.dev")
43-
headers["x-forwarded-host"] = headers["Host"]
44-
headers["Host"] = "app.shop.dev"
45-
end
42+
43+
parsed_uri = URI(request_url(request))
44+
45+
headers = append_first_party_development_headers(headers, parsed_uri)
4646

4747
tries = 0
4848
response = HttpResponse.new(code: 0, headers: {}, body: "")
4949
while tries < request.tries
5050
tries += 1
5151
res = T.cast(HTTParty.send(
5252
request.http_method,
53-
request_url(request),
53+
parsed_uri.to_s,
5454
headers: headers,
5555
query: request.query,
5656
body: request.body.class == Hash ? T.unsafe(request.body).to_json : request.body,
@@ -119,6 +119,27 @@ def serialized_error(response)
119119
end
120120
body.to_json
121121
end
122+
123+
private
124+
125+
sig do
126+
params(
127+
headers: T::Hash[T.any(Symbol, String), T.untyped],
128+
parsed_uri: URI::Generic,
129+
).returns(T::Hash[T.any(Symbol, String), T.untyped])
130+
end
131+
def append_first_party_development_headers(headers, parsed_uri)
132+
return headers unless defined?(DevServer)
133+
return headers unless headers["Host"]&.include?(".my.shop.dev") || parsed_uri.host&.include?(".my.shop.dev")
134+
135+
# These headers are only used for first party applications in development mode
136+
headers["x-forwarded-host"] = headers["Host"] || parsed_uri.host
137+
headers["Host"] = T.unsafe(
138+
Object.const_get("DevServer::Core"), # rubocop:disable Sorbet/ConstantsFromStrings
139+
).new.host!(:app)
140+
141+
headers
142+
end
122143
end
123144
end
124145
end

0 commit comments

Comments
 (0)