Skip to content

Commit aad9b01

Browse files
committed
refactor(html): Unified user agent HTTP requests
This unifies how requests are sent from; which enables a unified handling of redirect responses - Calling Nagivate (handles anchor tag clicks) - Form submit - Load a new window from location
1 parent 46a430f commit aad9b01

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

html/window.go

+16-30
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,7 @@ func OpenWindowFromLocation(location string, windowOptions ...WindowOption) (Win
126126
options.BaseLocation = location
127127
}
128128
result := newWindow(options)
129-
resp, err := result.httpClient.Get(location)
130-
if err != nil {
131-
return nil, err
132-
}
133-
if resp.StatusCode != 200 {
134-
return nil, errors.New("Non-ok Response")
135-
}
136-
err = result.parseReader(resp.Body)
137-
return result, err
129+
return result, result.get(location)
138130
}
139131

140132
func (w *window) initScriptEngine() {
@@ -217,14 +209,6 @@ func (w *window) Document() dom.Document {
217209
return w.document
218210
}
219211

220-
func (w *window) handleResponse(resp *http.Response) error {
221-
if resp.StatusCode != 200 {
222-
return errors.New("Non-ok Response")
223-
}
224-
return w.parseReader(resp.Body)
225-
226-
}
227-
228212
func (w *window) Navigate(href string) error {
229213
log.Info(w.Logger(), "Window.navigate:", "href", href)
230214
w.History().pushLoad(href)
@@ -234,11 +218,7 @@ func (w *window) Navigate(href string) error {
234218
w.document = NewHTMLDocument(w)
235219
return nil
236220
} else {
237-
resp, err := w.httpClient.Get(href)
238-
if err != nil {
239-
return err
240-
}
241-
return w.handleResponse(resp)
221+
return w.get(href)
242222
}
243223
}
244224

@@ -252,20 +232,26 @@ func (w *window) reload(href string) error {
252232
w.document = NewHTMLDocument(w)
253233
return nil
254234
} else {
255-
resp, err := w.httpClient.Get(href)
256-
if err != nil {
257-
return err
258-
}
259-
return w.handleResponse(resp)
235+
return w.get(href)
236+
}
237+
}
238+
239+
func (w *window) get(href string) (err error) {
240+
if req, err := http.NewRequest("GET", href, nil); err == nil {
241+
err = w.fetchRequest(req)
260242
}
243+
return err
261244
}
262245

263246
func (w *window) fetchRequest(req *http.Request) error {
264247
resp, err := w.httpClient.Do(req)
265-
if err == nil {
266-
err = w.handleResponse(resp)
248+
if err != nil {
249+
return err
267250
}
268-
return err
251+
if resp.StatusCode != 200 {
252+
return errors.New("Non-ok Response")
253+
}
254+
return w.parseReader(resp.Body)
269255
}
270256

271257
func (w *window) LoadHTML(html string) error {

0 commit comments

Comments
 (0)