openfaas / templates

OpenFaaS Classic templates
https://www.openfaas.com
MIT License
276 stars 228 forks source link

Java11 template: Response Status Code can't be changed #225

Open fakoe opened 3 years ago

fakoe commented 3 years ago

Expected Behaviour

Changing the status code of the response with res.setStatusCode(212) (similar to res.setBody()) should be possible.

Current Behaviour

Changing the status code doesn't change the response's status code. No matter what code is set, the java11 template always responds with "200".

Possible Solution

Wrong handling of the Status Code?

Steps to Reproduce (for bugs)

  1. Create java11 template
  2. Add line to Handler.java code: res.setStatusCode(202) or whatever number
  3. Build, push deploy java11 template
  4. Invoke or curl java11 function
  5. Get wireshark, or any other network sniffer and trace down the network traffic between openfaas and your pc

Context

I need custom status codes for http communication and I'm not able to set them properly.

I troubleshot the deployment (on Openshift) with using the write_debug: true setting in the .yaml-file. This is the log of my deployment:

2020/10/22 10:07:48 Started logging stderr from function.
2020/10/22 10:07:48 Started logging stdout from function.
2020/10/22 10:07:48 OperationalMode: http
2020/10/22 10:07:48 Timeouts: read: 10s, write: 10s hard: 10s.
2020/10/22 10:07:48 Listening on port: 8080
2020/10/22 10:07:48 Writing lock-file to: /tmp/.lock
2020/10/22 10:07:48 Metrics listening on port: 8081
2020/10/22 10:08:01 POST /function/java11 - 212  - ContentLength: 35
2020/10/22 10:08:01 stdout: Request / 35 bytes written.

As you can see, the code is set properly there (from 200 to 212). The problem is, when I log the network traffic between my client, which sends the curl (or invokes the function through the UI), this is the HTTP stream output (taken from wireshark):

GET /function/java11 HTTP/1.1
Host: <openfaas-ip>:8080
User-Agent: curl/7.61.1
Accept: */*

HTTP/1.1 200 OK
Content-Length: 35
Content-Type: text/plain; charset=utf-8
Date: Thu, 22 Oct 2020 11:03:07 GMT
X-Call-Id: 5fee7f67-3fd0-4262-9964-7bfd94a834c3
X-Duration-Seconds: 0.001307
X-Start-Time: 1603364587360704624

Hello, world! abcdefghijklmno

I guess the res.setStatusCode() is overwritten by the default handling of HTTP requests?

The response status in the UI of openfaas (under http://:8080) is also always set to 200, no matter what status code i set. Am I doing something wrong?

Your Environment