web request context

web request context

web request context

Function

The web service runtime injects web objects into the script. The web object consists of request data fields and response control functions. The specific structure is shown in the table below and the corresponding API page.

How to enable Web service please read: Web service

Request data

APIDescription
web.getRead URL query parameter object.
web.postRead form POST field object.
web.cookieRead the request Cookie object, and also write back the Cookie status after the request is completed.
web.sessionRead and save the current request Session object.
web.filesRead the uploaded file object.
web.serverRead request and server information.
web.urlRead the current request path.
web.methodRead the current HTTP request method.

Request object overview

APITypeStructure
web.getObjectQuery parameter name -> String.
web.postObjectForm field name -> String/Array.
web.cookieObjectCookie name -> String and write back the response cookie at the end of the request.
web.sessionObjectSession key name -> JSON serializable value.
web.filesObjectUpload field name -> file information object array; see web.files] for the file information field.
web.serverObjectmethod、version、scheme、headers、local_addr、remote_addr、ip、port。

Response Control

APIDescription
web.headerSet one or a group of response headers.
web.status_codeSet HTTP response status code.
web.redirectSet HTTP redirect address.
web.send_fileSend local files directly, suitable for large response downloads.

Blocking API strategy

Web request scripts will enter the BT process-level blocking pool for execution as a whole to avoid blocking Tokio Web workers. For the allow and deny rules for sleep, fs, process, reqwest, mysql, task, device in Web requests, see: Web blocking API policy .

Resource Boundaries

Dynamic web requests will check the request body, header, uploaded file and dynamic response body size. When the request or upload exceeds the limit, HTTP 413 is returned and a Chinese error is output; the dynamic response body exceeds the limit and the response writeback is terminated. Static files and web.send_file() files are sent in chunks by the web service and are not restricted by BT_WEB_RESPONSE_BODY_LIMIT.

Environment variableDefault valueDescription
BT_WEB_REQUEST_BODY_LIMIT16777216The upper limit of the request body, in bytes, including form and multipart uploads.
BT_WEB_RESPONSE_BODY_LIMIT67108864The upper limit of BT script dynamic response body, in bytes.
BT_WEB_HEADER_COUNT_LIMIT128Maximum number of headers requested.
BT_WEB_HEADER_BYTES_LIMIT65536Request the upper limit of cumulative bytes of header name and value.
BT_WEB_UPLOAD_FILE_LIMIT33554432The upper limit of the size of a single uploaded file, in bytes.

Examples

Notes

  • The web object only exists during the execution of the web request.
- Response control status is written to the HTTP response uniformly after the script execution ends.
  • Uploaded files will first pass the total request limit, and then pass the single file limit; for large file downloads, static files or web.send_file() files should be used first.
- Static files support ETag, Last-Modified, Conditional Request and Range; static cache policy is configured in net.listen static.cache_control of Web Service .
  • web.send_file() reuses the same set of file response capabilities, supporting ETag, Last-Modified, conditional requests and Range.