-
Notifications
You must be signed in to change notification settings - Fork 108
http: have sol_http_param_add_xxx functions to help build params #2249
Comments
I feel your pain, but note that there are two variants, one that copies the strings and the other that just references it :-/ |
ah, and in the past I've suggested a different approach since most of the time we add couple of fields that is to use an va_args. It would be bit less secure since one can always screw the order, but something like: static inline int sol_http_params_many_add(struct sol_http_params *params, ...);
sol_http_params_many_add(¶ms,
SOL_HTTP_REQUEST_PARAM_POST_FIELD("occupied", "yes"),
SOL_HTTP_REQUEST_PARAM_POST_FIELD("x", "1"),
SOL_HTTP_REQUEST_PARAM_SENTINEL /* 0 */); or (less safe): static inline int sol_http_params_many_add(struct sol_http_params *params, enum sol_http_param_type type, ...);
sol_http_params_many_add(¶ms, SOL_HTTP_PARAM_POST_FIELD,
"occupied", "yes",
"x", "1",
NULL); |
Is params expected to outlive the call to sol_http_client_request?
Side note: by far, most of my questions when reading the documentation were like the above. I wasn't sure whether was OK to create some parameter struct on the stack or not. |
The struct itself is not referenced, just the contents. They are kept around until the internals use it, as you can see, the API to add may be executed on one function that clears the stack and then the actual client post/get method is executed, this would use garbage memory. However, many times the strings will be alive, like static const globals or pointers we keep alive elsewhere (like machine-id or some other specific context that lives longer than the connection start). Then always duplicating is bad. For those we duplicate, we do keep an @cabelitos can you take a look to improve the docs? |
I meant its content as well. The sample code does something like:
When I copy, the sol_arena inside params get a copy of the strings, but then we clear them out right after the call. If they are needed "after" (i.e. imagine something posted to the mainloop for later execution), the strings will be already invalid as well. In this case it seems I should clear the params only during response time? It seems to me either the sample is "wrong" or the strings aren't needed after the do_request_with_params() returns, and we wouldn't need copy strings in the first place. (*) I say wrong because the sample doesn't copy anything, so the strings will always be valid. |
Once you OTOH, if you mean As for the actual implementation of As for the sample, you mean https://github.com/solettaproject/soletta/blob/master/src/samples/http/client.c? Looking at it, indeed, the copies shouldn't be needed. |
The example I was refering was the one in the docs
but the same sequence happen in
which is
Given that description of the implementation, it seems having an arena inside params and the copy variants aren't very helpful. |
This is an API suggestion.
The current way to build a params is by calling sol_http_params_add() with a helper macro that creates literals for the struct sol_http_param_value.
sol_http_params_add(¶ms, SOL_HTTP_REQUEST_PARAM_POST_FIELD("occupied", "yes"))
the problem with this approach is: we have to "pay" for the "namespace tax" twice, first for the function name, then for the macro name. It also adds a bit of smoke when the user does something wrong.
The suggestion is replace the macros to create literals for specific functions (they could be defined in the headers to enable inlining).
sol_http_params_add_post_field(¶ms, "occupied", "yes")
Similar pattern would apply for other types of parameters.
Note: I couldn't find uses of the SOL_HTTP_REQUEST_PARAM_* macros that were not associated with adding, are there any?
The text was updated successfully, but these errors were encountered: