sourcegraph / go-webkit2

WebKit API bindings (WebKitGTK+ v2) for Go
https://sourcegraph.com/github.com/sourcegraph/go-webkit2
Other
313 stars 61 forks source link

getting response headers #22

Open ftKnox opened 8 years ago

ftKnox commented 8 years ago

First let me preface by saying I am not a C coder and have been trying to figure this out for a bit. I am wanting to grab out the http response headers and put them into a map. I was able to figure out how to grab a single header by name, but have been unable to figure out how to grab out the entire list and convert into map. I looked through the gtk webkit2 docs and see it uses a callback function but I am unable to implement. Any help or insight would be appreciated.

This works to grab out a single named header:

func (v *WebView) GetResponseHeader(hdr string) string {
    return C.GoString(
        (*C.char)(
            C.soup_message_headers_get_list(
                (*C.SoupMessageHeaders)(
                    C.webkit_uri_response_get_http_headers(
                        (*C.WebKitURIResponse)(
                            C.webkit_web_resource_get_response(
                                (*C.WebKitWebResource)(
                                    C.webkit_web_view_get_main_resource(v.webView),
                                ),
                            ),
                        ),
                    ),
                ),
                C.CString(hdr),
            ),
        ),
    )
}

This is basically what i am trying to do

func (v *WebView) GetResponseHeaders() (headers map[string][]string) {

    var (
        has_key bool
    )

    headers = make(map[string][]string)

    build_headers := func(name, val string, user_data *C.gpointer) {
        if _, has_key = headers[name]; has_key {
            headers[name] = append(headers[name], val)
        } else {
            headers[name] = []string{val}
        }
    }

    _headers := (*C.SoupMessageHeaders)(
        C.webkit_uri_response_get_http_headers(
            (*C.WebKitURIResponse)(
                C.webkit_web_resource_get_response(
                    (*C.WebKitWebResource)(
                        C.webkit_web_view_get_main_resource(v.webView),
                    ),
                ),
            ),
        ),
    )

    C.soup_message_headers_foreach(
        _headers,
        (*C.SoupMessageHeadersForeachFunc)(build_headers),
        nil,
    )

    C.free(unsafe.Pointer(_headers))

    return
}

which returns:

./src/github.com/sourcegraph/go-webkit2/webkit2/webview.go:119: cannot convert func literal (type func(string, string, *C.gpointer)) to type *C.SoupMessageHeadersForeachFunc
../src/github.com/sourcegraph/go-webkit2/webkit2/webview.go:137: cannot use build_headers (type *C.SoupMessageHeadersForeachFunc) as type *[0]byte in argument to _Cfunc_soup_message_headers_foreach