I am developing a https redirect feature of our product. To test https redirect I used python to setup a simple testing webserver that could generate https redirect response. But when I test using curl I always get an error
curl: (56) Failure when receiving data from the peer
import http.server
import socketserver
import ssl
PORT = 8000
REDIRECTS = {
"/redirect1": {"status": 301, "url": "https://www.google.com"},
"/redirect2": {"status": 302, "url": "https://www.bing.com"},
# Add more redirect paths and their corresponding status and URLs as needed
class RedirectHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
redirect_info = REDIRECTS.get(self.path)
if redirect_info:
status_code = redirect_info["status"]
redirect_url = redirect_info["url"]
self.send_response(status_code)
self.send_header("Location", redirect_url)
self.end_headers()
return
super().do_GET()
def do_POST(self):
redirect_info = REDIRECTS.get(self.path)
if redirect_info:
status_code = redirect_info["status"]
redirect_url = redirect_info["url"]
self.send_response(status_code)
self.send_header("Location", redirect_url)
self.end_headers()
return
super().do_POST()
if __name__ == "__main__":
# Generate a self-signed SSL certificate for testing purposes
certfile = "cert.pem"
keyfile = "key.pem"
httpd = socketserver.TCPServer(("", PORT), RedirectHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=certfile, keyfile=keyfile, server_side=True)
print(f"Server started on port {PORT}")
httpd.serve_forever()
The self-signed cert.pem and key.pem are generated by openssl on same Windows host.
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
The python webserver is started by python redirect_server.py
on same local host. Then the curl command always fails. After open verbose flag I see this. Seems it is caused by * schannel: server closed abruptly (missing close_notify)
. Any idea about how to fix it? Thanks in advance!
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
* schannel: disabled automatic use of client certificate
* ALPN: offers http/1.1
* ALPN: server did not agree on a protocol. Uses default.
* using HTTP/1.x
> POST /redirect1 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/8.0.1
> Accept: */*
* HTTP 1.0, assume close after body
< HTTP/1.0 301 Moved Permanently
< Server: SimpleHTTP/0.6 Python/3.10.9
< Date: Thu, 03 Aug 2023 23:17:38 GMT
< Location: https://www.google.com
* schannel: server closed abruptly (missing close_notify)
* Closing connection 0
* schannel: shutting down SSL/TLS connection with localhost port 8000
curl: (56) Failure when receiving data from the peer
I am running on Windows 10
Edition Windows 10 Pro
Version 22H2
OS build 19045.3208
Experience Windows Feature Experience Pack 1000.19041.1000.0
Best,
libcurl must receive a termination point to know when to stop reading. For example Content Length 0 eg self.send_header("Content-Length", 0)
. If there is no HTTP termination point and the connection is secure (HTTPS), then libcurl checks that the entire body is received by checking for a close_notify message from the server. It appears this example server does not send that. Try adding the content length header.
Also, you are doing POST wrong please see:
UNNECESSARY USE OF CURL -X
curl manual -d, --data and other --data variants on the same page
edit: also you need to use --location for redirect
libcurl must receive a termination point to know when to stop reading. For example Content Length 0 eg self.send_header("Content-Length", 0)
. If there is no HTTP termination point and the connection is secure (HTTPS), then libcurl checks that the entire body is received by checking for a close_notify message from the server. It appears this example server does not send that. Try adding the content length header.
Also, you are doing POST wrong please see:
UNNECESSARY USE OF CURL -X
curl manual -d, --data and other --data variants on the same page
edit: also you need to use --location for redirect