Oxygen/HTTP – I have a problem with CORS handling

On Windows Server 2022 I have an API that is based on Julia. Everything works fine except CORS handling. I have the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using Oxygen
using HTTP
...
function CorsMiddleware(handler)
return function(req::HTTP.Request)
println("CORS middleware")
# Log the request headers
println("Request Headers -> ", req.headers)
# Handle preflight requests (OPTIONS)
if HTTP.method(req) == "OPTIONS"
return HTTP.Response(200, "Preflight response")
else
try
# Call the next handler
res = handler(req)
# Log the response body
println("CorsMiddleware: Response Body -> ", res.body)
# Return a basic response without headers
return HTTP.Response(res.status, res.body)
catch e
println("Error in CorsMiddleware when calling handler(req): ", e)
return HTTP.Response(500, "Internal Server Error")
end
end
end
end
function AuthMiddleware(handler)
return function(req::HTTP.Messages.Request)
println("Auth middleware")
try
# Call the next handler
res = handler(req)
# Log the response body
println("AuthMiddleware: Response Body -> ", res.body)
# Return a basic response without any headers
return HTTP.Response(res.status, res.body)
catch e
println("Error in AuthMiddleware when calling handler(req): ", e)
return HTTP.Response(500, "Internal Server Error")
end
end
end
...
serve(port=4091, middleware=[CorsMiddleware, AuthMiddleware])
</code>
<code>using Oxygen using HTTP ... function CorsMiddleware(handler) return function(req::HTTP.Request) println("CORS middleware") # Log the request headers println("Request Headers -> ", req.headers) # Handle preflight requests (OPTIONS) if HTTP.method(req) == "OPTIONS" return HTTP.Response(200, "Preflight response") else try # Call the next handler res = handler(req) # Log the response body println("CorsMiddleware: Response Body -> ", res.body) # Return a basic response without headers return HTTP.Response(res.status, res.body) catch e println("Error in CorsMiddleware when calling handler(req): ", e) return HTTP.Response(500, "Internal Server Error") end end end end function AuthMiddleware(handler) return function(req::HTTP.Messages.Request) println("Auth middleware") try # Call the next handler res = handler(req) # Log the response body println("AuthMiddleware: Response Body -> ", res.body) # Return a basic response without any headers return HTTP.Response(res.status, res.body) catch e println("Error in AuthMiddleware when calling handler(req): ", e) return HTTP.Response(500, "Internal Server Error") end end end ... serve(port=4091, middleware=[CorsMiddleware, AuthMiddleware]) </code>
using Oxygen
using HTTP

...

function CorsMiddleware(handler)
    return function(req::HTTP.Request)
        println("CORS middleware")
        
        # Log the request headers
        println("Request Headers -> ", req.headers)
        
        # Handle preflight requests (OPTIONS)
        if HTTP.method(req) == "OPTIONS"
            return HTTP.Response(200, "Preflight response")
        else
            try
                # Call the next handler
                res = handler(req)
                
                # Log the response body
                println("CorsMiddleware: Response Body -> ", res.body)
                
                # Return a basic response without headers
                return HTTP.Response(res.status, res.body)
            catch e
                println("Error in CorsMiddleware when calling handler(req): ", e)
                return HTTP.Response(500, "Internal Server Error")
            end
        end
    end
end

function AuthMiddleware(handler)
    return function(req::HTTP.Messages.Request)
        println("Auth middleware")
        
        try
            # Call the next handler
            res = handler(req)
            
            # Log the response body
            println("AuthMiddleware: Response Body -> ", res.body)
            
            # Return a basic response without any headers
            return HTTP.Response(res.status, res.body)
        catch e
            println("Error in AuthMiddleware when calling handler(req): ", e)
            return HTTP.Response(500, "Internal Server Error")
        end
    end
end

...

serve(port=4091, middleware=[CorsMiddleware, AuthMiddleware])

I added the println statements to see why things don’t work. This is the output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CORS middleware
Request Headers -> Pair{SubString{String},
SubString{String}}["Host" => "localhost:4091",
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0", "Accept" => "*/*",
"Accept-Language" => "en-US,en;q=0.5",
"Accept-Encoding" => "gzip, deflate, br, zstd",
"Content-Type" => "text/plain", "Content-Length" => "22",
"Origin" => "https://viewer.trexyz.net",
"Connection" => "keep-alive",
"Sec-Fetch-Dest" => "empty",
"Sec-Fetch-Mode" => "cors",
"Sec-Fetch-Site" => "cross-site",
"Priority" => "u=4"]
Auth middleware
AuthMiddleware: Response Body -> UInt8[0x5b, 0x22, 0x4d, 0x69, 0x6c, 0x6c, 0x73, 0x5f, 0x32, 0x22, 0x2c, 0x22, 0x4b, 0x69, 0x70, 0x70, 0x65, 0x6e, 0x5f, 0x42, 0x22, 0x2c, 0x22, 0x4b, 0x69, 0x70, 0x70, 0x65, 0x6e, 0x5f, 0x41, 0x22, 0x5d]
Error in AuthMiddleware when calling handler(req): ArgumentError("invalid header key-value pair: 91")
CorsMiddleware: Response Body -> UInt8[0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72]
Error in CorsMiddleware when calling handler(req): ArgumentError("invalid header key-value pair: 73")
</code>
<code>CORS middleware Request Headers -> Pair{SubString{String}, SubString{String}}["Host" => "localhost:4091", "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0", "Accept" => "*/*", "Accept-Language" => "en-US,en;q=0.5", "Accept-Encoding" => "gzip, deflate, br, zstd", "Content-Type" => "text/plain", "Content-Length" => "22", "Origin" => "https://viewer.trexyz.net", "Connection" => "keep-alive", "Sec-Fetch-Dest" => "empty", "Sec-Fetch-Mode" => "cors", "Sec-Fetch-Site" => "cross-site", "Priority" => "u=4"] Auth middleware AuthMiddleware: Response Body -> UInt8[0x5b, 0x22, 0x4d, 0x69, 0x6c, 0x6c, 0x73, 0x5f, 0x32, 0x22, 0x2c, 0x22, 0x4b, 0x69, 0x70, 0x70, 0x65, 0x6e, 0x5f, 0x42, 0x22, 0x2c, 0x22, 0x4b, 0x69, 0x70, 0x70, 0x65, 0x6e, 0x5f, 0x41, 0x22, 0x5d] Error in AuthMiddleware when calling handler(req): ArgumentError("invalid header key-value pair: 91") CorsMiddleware: Response Body -> UInt8[0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72] Error in CorsMiddleware when calling handler(req): ArgumentError("invalid header key-value pair: 73") </code>
CORS middleware
Request Headers -> Pair{SubString{String}, 
SubString{String}}["Host" => "localhost:4091", 
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0", "Accept" => "*/*", 
"Accept-Language" => "en-US,en;q=0.5", 
"Accept-Encoding" => "gzip, deflate, br, zstd", 
"Content-Type" => "text/plain", "Content-Length" => "22", 
"Origin" => "https://viewer.trexyz.net", 
"Connection" => "keep-alive", 
"Sec-Fetch-Dest" => "empty", 
"Sec-Fetch-Mode" => "cors", 
"Sec-Fetch-Site" => "cross-site", 
"Priority" => "u=4"]

Auth middleware
AuthMiddleware: Response Body -> UInt8[0x5b, 0x22, 0x4d, 0x69, 0x6c, 0x6c, 0x73, 0x5f, 0x32, 0x22, 0x2c, 0x22, 0x4b, 0x69, 0x70, 0x70, 0x65, 0x6e, 0x5f, 0x42, 0x22, 0x2c, 0x22, 0x4b, 0x69, 0x70, 0x70, 0x65, 0x6e, 0x5f, 0x41, 0x22, 0x5d]
Error in AuthMiddleware when calling handler(req): ArgumentError("invalid header key-value pair: 91")
CorsMiddleware: Response Body -> UInt8[0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72]
Error in CorsMiddleware when calling handler(req): ArgumentError("invalid header key-value pair: 73")

The invalid header errors keep coming, no matter what I try – like stricter header handling, and using hardcoded headers. Even a completely stripped test like this gives a 500 error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using HTTP
function simple_handler(req::HTTP.Request)
println("Simple handler")
# Create a basic response body
response_body = "{"message": "Hello, world!"}"
# Return a basic response without headers
return HTTP.Response(200, response_body)
</code>
<code>using HTTP function simple_handler(req::HTTP.Request) println("Simple handler") # Create a basic response body response_body = "{"message": "Hello, world!"}" # Return a basic response without headers return HTTP.Response(200, response_body) </code>
using HTTP

function simple_handler(req::HTTP.Request)
    println("Simple handler")
    
    # Create a basic response body
    response_body = "{"message": "Hello, world!"}"
    
    # Return a basic response without headers
    return HTTP.Response(200, response_body)

Does anyone have any idea what’s going wrong here?

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật