mafredri / cdp

Package cdp provides type-safe bindings for the Chrome DevTools Protocol (CDP), written in the Go programming language.
MIT License
725 stars 45 forks source link

RequestIntercepted can't set respond #119

Closed wanhl1990 closed 4 years ago

wanhl1990 commented 4 years ago

Hi mafredri, I had a new issue. Does RequestIntercepted method set the respond ? I saw pyppeteer can do this.

async def intercept_request(request: Request):
    if request.isNavigationRequest() and not request.frame.parentFram:
        await request.respond({
            "status": 503
        })

page.on('request', lambda request: asyncio.ensure_future(
            intercept_request(request)))

I only found SetRawResponse in ContinueInterceptedRequest args. But when I set this, receive net::ERR_ABORTED error.

Also get the RequestPaused method. It has a FulfillRequest. But it does not have isNavigationRequest likes method.

Could you give some advice? I want to check the request is NavigationRequest or not. And fulfills request with given response.

Thanks your package. It give me a lot of help.

mafredri commented 4 years ago

I believe you should be able to use RawResponse for this purpose. Here's an example of how you'd use it:

r, err := requestIntercepted.Recv()
if err != nil {
    panic(err)
}

body := "Please try again later."
rawResponse := strings.Join(
    []string{
        fmt.Sprintf("HTTP/1.1 200 %d", 503, "Service Unavailable"),
        "Connection: closed",
        fmt.Sprintf("Content-Length: %d", len(body)),
        "Content-Type: text/plain",
        "",
        body,
    },
    "\r\n",
)

interceptedArgs := network.NewContinueInterceptedRequestArgs(r.InterceptionID)
interceptedArgs.SetRawResponse(
    base64.StdEncoding.EncodeToString([]byte(rawResponse)),
)
if err = c.Network.ContinueInterceptedRequest(ctx, interceptedArgs); err != nil {
    panic(err)
}
wanhl1990 commented 4 years ago

OK, Thanks your way. I don't have a deep understanding before. Thanks a lot. Have a good time.