Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assetserver_webview.go request cancelation on MacOS #3632

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v2/internal/frontend/desktop/darwin/WailsContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ - (void)webView:(nonnull WKWebView *)webView startURLSchemeTask:(nonnull id<WKUR
}

- (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id<WKURLSchemeTask>)urlSchemeTask {
processURLRequestStop(self, urlSchemeTask);
NSInputStream *stream = urlSchemeTask.request.HTTPBodyStream;
if (stream) {
NSStreamStatus status = stream.streamStatus;
Expand Down
11 changes: 11 additions & 0 deletions v2/internal/frontend/desktop/darwin/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,17 @@ func processURLRequest(_ unsafe.Pointer, wkURLSchemeTask unsafe.Pointer) {
requestBuffer <- webview.NewRequest(wkURLSchemeTask)
}

//export processURLRequestStop
func processURLRequestStop(_ unsafe.Pointer, wkURLSchemeTask unsafe.Pointer) {
r := webview.NewRequest(wkURLSchemeTask)
requestID, err := r.URL()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requestID shouldn't just be the r.URL() , it should also contain the r.Method().

if err != nil {
fmt.Println(err.Error())
}

assetserver.CancelRequest(requestID)
}

//export HandleOpenFile
func HandleOpenFile(filePath *C.char) {
goFilepath := C.GoString(filePath)
Expand Down
1 change: 1 addition & 0 deletions v2/internal/frontend/desktop/darwin/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C"

void processMessage(const char *);
void processURLRequest(void *, void*);
void processURLRequestStop(void *, void*);
void processMessageDialogResponse(int);
void processOpenFileDialogResponse(const char*);
void processSaveFileDialogResponse(const char*);
Expand Down
21 changes: 20 additions & 1 deletion v2/pkg/assetserver/assetserver_webview.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assetserver

import (
"context"
"fmt"
"net/http"
"net/url"
Expand All @@ -20,6 +21,8 @@ type assetServerWebView struct {
dispatchWorkers int
}

var requestCancelFuncsMap sync.Map

// ServeWebViewRequest processes the HTTP Request asynchronously by faking a golang HTTP Server.
// The request will be finished with a StatusNotImplemented code if no handler has written to the response.
// The AssetServer takes ownership of the request and the caller mustn't close it or access it in any other way.
Expand Down Expand Up @@ -52,6 +55,13 @@ func (d *AssetServer) ServeWebViewRequest(req webview.Request) {
}
}

func CancelRequest(requestID string) {
if cancelFunc, ok := requestCancelFuncsMap.Load(requestID); ok {
cancelFunc.(context.CancelFunc)()
requestCancelFuncsMap.Delete(requestID)
}
}

func (d *AssetServer) processWebViewRequest(r webview.Request) {
uri, _ := r.URL()
d.processWebViewRequestInternal(r)
Expand Down Expand Up @@ -106,7 +116,16 @@ func (d *AssetServer) processWebViewRequestInternal(r webview.Request) {
}
defer body.Close()

req, err := http.NewRequest(method, uri, body)
requestID := uri
ctx, cancelFunc := context.WithCancel(context.Background())
requestCancelFuncsMap.Store(requestID, cancelFunc)

defer func() {
cancelFunc()
requestCancelFuncsMap.Delete(requestID)
}()

req, err := http.NewRequestWithContext(ctx, method, uri, body)
if err != nil {
d.webviewRequestErrorHandler(uri, rw, fmt.Errorf("HTTP-Request: %w", err))
return
Expand Down