{"id":49626981,"url":"https://github.com/cornejong/cooper","last_synced_at":"2026-05-05T08:05:33.553Z","repository":{"id":347140747,"uuid":"1192994062","full_name":"cornejong/cooper","owner":"cornejong","description":"simple http hijacking for golang","archived":false,"fork":false,"pushed_at":"2026-03-26T19:32:39.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-27T08:19:59.014Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cornejong.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-03-26T19:09:35.000Z","updated_at":"2026-03-26T19:32:43.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/cornejong/cooper","commit_stats":null,"previous_names":["cornejong/cooper"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/cornejong/cooper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cornejong%2Fcooper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cornejong%2Fcooper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cornejong%2Fcooper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cornejong%2Fcooper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cornejong","download_url":"https://codeload.github.com/cornejong/cooper/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cornejong%2Fcooper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32640541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-04T10:08:07.713Z","status":"online","status_checked_at":"2026-05-05T02:00:06.033Z","response_time":54,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-05T08:05:32.759Z","updated_at":"2026-05-05T08:05:33.548Z","avatar_url":"https://github.com/cornejong.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cooper\n\nSimple HTTP hijacking for Go.\n\nDoes the `101 Switching Protocols` handshake, deals with buffered bytes, and gives you a `net.Conn`. That's it.\n\n```sh\ngo get github.com/cornejong/cooper\n```\n\n## Server side\n\n`cooper.Hijack` gives you an `http.Handler`. It does the upgrade handshake and hands you the raw connection:\n\n```go\nhandler := cooper.Hijack(func(conn net.Conn, proto string) {\n    defer conn.Close()\n    fmt.Fprintf(conn, \"hello over %s!\\n\", proto)\n}, \"myproto/1\")\n\nhttp.Handle(\"/connect\", handler)\nhttp.ListenAndServe(\":8080\", nil)\n```\n\nPass one or more protocol names to restrict what the server accepts. If none are passed, any `Upgrade` value goes through. Protocol matching is case-insensitive. Clients requesting something not in the list get a `426 Upgrade Required`.\n\nThe handler receives the connection and is responsible for closing it.\n\n## Client side\n\n`cooper.Upgrade` does the client half over an existing `net.Conn`:\n\n```go\nconn, _ := net.Dial(\"tcp\", \"localhost:8080\")\n\nreq, _ := http.NewRequest(\"GET\", \"http://localhost:8080/connect\", nil)\nreq.Header.Set(\"Upgrade\", \"myproto/1\")\n\nupgraded, err := cooper.Upgrade(conn, req)\nif err != nil {\n    panic(err)\n}\ndefer upgraded.Close()\n\nbuf := make([]byte, 1024)\nn, _ := upgraded.Read(buf)\nfmt.Println(string(buf[:n]))\n```\n\nYou build the `*http.Request` yourself. The only requirement is that it has an `Upgrade` header set. `Connection: Upgrade` gets added if you forget it.\n\nThere's a 10 second timeout on the handshake. If the server doesn't respond with `101`, or the `Upgrade`/`Connection` headers in the response don't check out, you get an error back.\n\n## Buffered bytes\n\nGo's HTTP server (and `bufio.Reader` on the client side) might read past the end of the handshake into your protocol data. Cooper handles this internally with a `prefixConn` wrapper that drains those buffered bytes first before reading from the underlying connection. You don't need to think about it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcornejong%2Fcooper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcornejong%2Fcooper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcornejong%2Fcooper/lists"}