Add http.put (#3462)

Replaces setpostdata() with setbody().
This commit is contained in:
Marcel Stör 2021-10-05 04:33:27 +02:00 committed by GitHub
parent ea968de6f8
commit 389c119a8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 18 deletions

View File

@ -25,7 +25,7 @@ enum {
// after this are other refs which aren't callbacks
ContextRef,
PostDataRef,
BodyDataRef,
CertRef,
CountRefs // Must be last
};
@ -568,17 +568,16 @@ static int http_lapi_setheader(lua_State *L)
return 0;
}
// context:setpostdata(data)
static int http_lapi_setpostdata(lua_State *L)
// context:setbody(data)
static int http_lapi_setbody(lua_State *L)
{
lhttp_context_t *context = (lhttp_context_t *)luaL_checkudata(L, 1, http_context_mt);
CHECK_CONNECTION_IDLE(context);
size_t postdata_sz;
const char *postdata = luaL_optlstring(L, 2, NULL, &postdata_sz);
esp_http_client_set_method(context->client, HTTP_METHOD_POST);
esp_http_client_set_post_field(context->client, postdata, (int)postdata_sz);
context_setref(L, context, PostDataRef);
size_t data_sz;
const char *data = luaL_optlstring(L, 2, NULL, &data_sz);
esp_http_client_set_post_field(context->client, data, (int)data_sz);
context_setref(L, context, BodyDataRef);
return 0;
}
@ -770,7 +769,44 @@ static int http_lapi_post(lua_State *L)
luaL_pcallx(L, 3, 1); // returns context
lua_pushcfunction(L, http_lapi_setpostdata);
lua_pushcfunction(L, http_lapi_setbody);
lua_pushvalue(L, -2); // context
lua_pushvalue(L, 3); // body
lua_call(L, 2, 0);
return make_oneshot_request(L, 4); // 4 = callback idx
}
// http.put(url, options, body[, callback])
static int http_lapi_put(lua_State *L)
{
lua_settop(L, 4);
luaL_checkstring(L, 1);
if (lua_isnil(L, 2)) {
lua_newtable(L);
lua_replace(L, 2);
}
// Now 1 = url, 2 = non-nil options, 3 = body, 4 = [callback]
luaL_argcheck(L, lua_istable(L, 2), 2, "options must be nil or a table");
luaL_checkstring(L, 3);
bool async = lua_isfunction(L, 4);
luaL_argcheck(L, lua_isnil(L, 4) || async, 4, "callback must be nil or a function");
// Override options.async based on whether callback present
lua_pushboolean(L, async);
lua_setfield(L, 2, "async");
// Setup call to createConnection
lua_pushcfunction(L, http_lapi_createConnection);
lua_pushvalue(L, 1); // url
lua_pushinteger(L, HTTP_METHOD_PUT);
lua_pushvalue(L, 2); // options
lua_call(L, 3, 1); // returns context
lua_pushcfunction(L, http_lapi_setbody);
lua_pushvalue(L, -2); // context
lua_pushvalue(L, 3); // body
luaL_pcallx(L, 2, 0);
@ -782,12 +818,14 @@ LROT_BEGIN(http, NULL, 0)
LROT_FUNCENTRY(createConnection, http_lapi_createConnection)
LROT_NUMENTRY (GET, HTTP_METHOD_GET)
LROT_NUMENTRY (POST, HTTP_METHOD_POST)
LROT_NUMENTRY (PUT, HTTP_METHOD_PUT)
LROT_NUMENTRY (DELETE, HTTP_METHOD_DELETE)
LROT_NUMENTRY (HEAD, HTTP_METHOD_HEAD)
LROT_NUMENTRY (DELAYACK, DELAY_ACK)
LROT_NUMENTRY (ACKNOW, 0) // Doesn't really matter what this is
LROT_FUNCENTRY(get, http_lapi_get)
LROT_FUNCENTRY(post, http_lapi_post)
LROT_FUNCENTRY(put, http_lapi_put)
LROT_END(http, NULL, 0)
LROT_BEGIN(http_context, NULL, LROT_MASK_GC_INDEX)
@ -798,7 +836,7 @@ LROT_BEGIN(http_context, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY(setmethod, http_lapi_setmethod)
LROT_FUNCENTRY(setheader, http_lapi_setheader)
LROT_FUNCENTRY(seturl, http_lapi_seturl)
LROT_FUNCENTRY(setpostdata, http_lapi_setpostdata)
LROT_FUNCENTRY(setbody, http_lapi_setbody)
LROT_FUNCENTRY(close, context_close)
LROT_FUNCENTRY(ack, http_lapi_ack)
LROT_END(http_context, NULL, LROT_MASK_GC_INDEX)

View File

@ -20,7 +20,7 @@ Creates a connection object which can be configured and then executed. Note this
#### Parameters
- `url` The URL to fetch, including the `http://` or `https://` prefix. Required.
- `method` The HTTP method to use, one of `http.GET`, `http.POST`, `http.DELETE` or `http.HEAD`. Optional and may be omitted, the default is `http.GET`.
- `method` The HTTP method to use, one of `http.GET`, `http.POST`, `http.PUT`, `http.DELETE` or `http.HEAD`. Optional and may be omitted, the default is `http.GET`.
- `options` An optional table containing any or all of:
- `async` If true, the request is processed asynchronously, meaning [`request()`](#connectionrequest) returns immediately rather than blocking until the connection is complete and all callbacks have been made. Some other connection APIs behave differently in asynchronous mode, see their documentation for details. If not specified, the default is `false`, meaning requests are processed synchronously.
- `bufsz` The size in bytes of the temporary buffer used for reading data. If not specified, the default is `512`.
@ -45,7 +45,7 @@ end)
connection:request()
```
# http connection objects
# HTTP connection objects
## connection:on()
Set a callback to be called when a certain event occurs.
@ -94,7 +94,7 @@ Sets the connection method. Useful if making multiple requests of different type
`connection:setmethod(method)`
#### Parameters
- `method` one of `http.GET`, `http.POST`, `http.HEAD`, `http.DELETE`.
- `method` one of `http.GET`, `http.POST`, `http.PUT`, `http.HEAD`, `http.DELETE`.
#### Returns
`nil`
@ -135,14 +135,14 @@ Sets an individual header in the request. Header names are case-insensitive, but
- `name` name of the header to set.
- `value` what to set it to. Must be a string, or `nil` to unset it.
## connection:setpostdata()
Sets the POST data to be used for this request. Also sets the method to `http.POST` if it isn't already. If a `Content-Type` header has not already been set, also sets that to `application/x-www-form-urlencoded`. Errors if called while a request is in progress.
## connection:setbody()
Sets the body data to be used for this request (for POST, PUT, etc). If a `Content-Type` header has not already been set, also sets that to `application/x-www-form-urlencoded`. Errors if called while a request is in progress.
#### Syntax
`connection:setpostdata([data])`
`connection:setbody([data])`
#### Parameters
`data` - The data to POST. Unless a custom `Content-Type` header has been set, this data should be in `application/x-www-form-urlencoded` format. Can be `nil` to unset what to post and the `Content-Type` header.
`data` The data to POST/PUT/etc.. Unless a custom `Content-Type` header has been set, this data should be in `application/x-www-form-urlencoded` format. Can be `nil` to unset what to post and the `Content-Type` header.
#### Returns
`nil`
@ -225,7 +225,7 @@ Executes a single HTTP POST request and closes the connection. If a `callback` i
#### Parameters
- `url` The URL to fetch, including the `http://` or `https://` prefix
- `options` Same options as [`http.createConnection()`](#httpcreateconnection), except that `async` is set for you based on whether a `callback` is specified or not. May be `nil`.
- `body` The body to post. Required and must already be encoded in the appropriate format, but may be empty. See [`connection:setpostdata()`](#connectionsetpostdata) for more information.
- `body` The body to post. Required and must already be encoded in the appropriate format, but may be empty. See [`connection:setbody()`](#connectionsetbody) for more information.
- `callback` Should be `nil` or omitted to specify synchronous mode, otherwise a callback function to be invoked when the response has been received or an error occurred, which is called with the arguments `status_code`, `body` and `headers`. In case of an error `status_code` will be a negative number.
#### Returns
@ -246,3 +246,19 @@ http.post("http://httpbin.org/post", { headers = headers }, body,
end
end)
```
## http.put()
Executes a single HTTP PUT request and closes the connection. If a `callback` is specifed then the function operates in asynchronous mode, otherwise it is synchronous.
#### Syntax
`http.put(url, options, body[, callback])`
#### Parameters
- `url` The URL to fetch, including the `http://` or `https://` prefix
- `options` Same options as [`http.createConnection()`](#httpcreateconnection), except that `async` is set for you based on whether a `callback` is specified or not. May be `nil`.
- `body` The body to post. Required and must already be encoded in the appropriate format, but may be empty. See [`connection:setbody()`](#connectionsetbody) for more information.
- `callback` Should be `nil` or omitted to specify synchronous mode, otherwise a callback function to be invoked when the response has been received or an error occurred, which is called with the arguments `status_code`, `body` and `headers`. In case of an error `status_code` will be a negative number.
#### Returns
In synchronous mode, returns 3 results `status_code, body, headers` once the request has completed. In asynchronous mode, returns `nil` immediately.