diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2005-04-16 05:09:33 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2005-04-16 05:09:33 +0000 |
commit | c17dc661eae89cea66959fad4fe48d77f1faf174 (patch) | |
tree | 1909b4157137e66b3f74adc75ca1a2bbf3bc3c6f /render | |
parent | 37a4119ab80b1a7c7e37707c4e029218bdcbe549 (diff) | |
download | netsurf-c17dc661eae89cea66959fad4fe48d77f1faf174.tar.gz netsurf-c17dc661eae89cea66959fad4fe48d77f1faf174.tar.bz2 |
[project @ 2005-04-16 05:09:32 by jmb]
Split out UTF-8 handling functions.
Submit URL-encoded forms in sensible encoding:
* First entry in accept-charset list, if present
* Document encoding, otherwise
We may want to explicitly look for UTF-8, to save converting.
Convert cnv_str_local_enc/cnv_local_enc_str to use iconv (they're now veneers for utf8_[to/from]_enc).
Provide mechanism for looking up local system charset (derived from system alphabet, under RISC OS)
svn path=/import/netsurf/; revision=1647
Diffstat (limited to 'render')
-rw-r--r-- | render/box_construct.c | 30 | ||||
-rw-r--r-- | render/form.c | 35 | ||||
-rw-r--r-- | render/form.h | 6 |
3 files changed, 59 insertions, 12 deletions
diff --git a/render/box_construct.c b/render/box_construct.c index 9723b4de1..88a432e67 100644 --- a/render/box_construct.c +++ b/render/box_construct.c @@ -1575,7 +1575,7 @@ bool box_iframe(BOX_SPECIAL_PARAMS) bool box_form(BOX_SPECIAL_PARAMS) { - char *action, *method, *enctype; + char *action, *method, *enctype, *charset; form_method fmethod; struct form *form; @@ -1598,9 +1598,35 @@ bool box_form(BOX_SPECIAL_PARAMS) xmlFree(method); } - form = form_new(action, fmethod); + /* acceptable encoding(s) for form data */ + if ((charset = (char *) xmlGetProp(n, (const xmlChar *) "accept-charset"))) { + char *comma = strchr(charset, ','); + if (!comma) + /* only one => use it */ + comma = strdup(charset); + else + /* multiple => use first */ + comma = strndup(charset, comma - charset); + + xmlFree(charset); + charset = comma; + } + else if (content->data.html.encoding) + /* none specified => try document encoding */ + charset = strdup(content->data.html.encoding); + else + /* none specified and no document encoding => 8859-1 */ + charset = strdup("ISO-8859-1"); + + if (!charset) { + xmlFree(action); + return false; + } + + form = form_new(action, fmethod, charset); if (!form) { xmlFree(action); + free(charset); return false; } form->prev = content->data.html.forms; diff --git a/render/form.c b/render/form.c index 559f7e3e2..1cc7d0840 100644 --- a/render/form.c +++ b/render/form.c @@ -17,21 +17,22 @@ #include "netsurf/render/box.h" #include "netsurf/render/form.h" #include "netsurf/utils/log.h" +#include "netsurf/utils/utf8.h" #include "netsurf/utils/utils.h" static char *form_textarea_value(struct form_control *textarea); - /** * Create a struct form. * * \param action URL to submit form to, used directly (not copied) * \param method method and enctype + * \param charset characterset of form (not copied) * \return a new structure, or 0 on memory exhaustion */ -struct form *form_new(char *action, form_method method) +struct form *form_new(char *action, form_method method, char *charset) { struct form *form; @@ -40,6 +41,7 @@ struct form *form_new(char *action, form_method method) return 0; form->action = action; form->method = method; + form->charset = charset; form->controls = 0; form->last_control = 0; form->prev = 0; @@ -465,15 +467,15 @@ char *form_textarea_value(struct form_control *textarea) /** * Encode controls using application/x-www-form-urlencoded. * + * \param form form to which successful controls relate * \param control linked list of form_successful_control * \return URL-encoded form, or 0 on memory exhaustion - * - * \todo encoding conversion */ -char *form_url_encode(struct form_successful_control *control) +char *form_url_encode(struct form *form, + struct form_successful_control *control) { - char *name, *value; + char *name, *value, *n_temp, *v_temp; char *s = malloc(1), *s2; unsigned int len = 0, len1; @@ -482,11 +484,26 @@ char *form_url_encode(struct form_successful_control *control) s[0] = 0; for (; control; control = control->next) { - name = curl_escape(control->name, 0); - value = curl_escape(control->value, 0); + n_temp = utf8_to_enc(control->name, form->charset, 0); + if (!n_temp) { + free(s); + return 0; + } + v_temp = utf8_to_enc(control->value, form->charset, 0); + if (!v_temp) { + free(n_temp); + free(s); + return 0; + } + name = curl_escape(n_temp, 0); + value = curl_escape(v_temp, 0); len1 = len + strlen(name) + strlen(value) + 2; s2 = realloc(s, len1 + 1); if (!s2) { + curl_free(value); + curl_free(name); + free(v_temp); + free(n_temp); free(s); return 0; } @@ -495,6 +512,8 @@ char *form_url_encode(struct form_successful_control *control) len = len1; curl_free(name); curl_free(value); + free(v_temp); + free(n_temp); } if (len) s[len - 1] = 0; diff --git a/render/form.h b/render/form.h index ceddd96f8..c1e01ae0a 100644 --- a/render/form.h +++ b/render/form.h @@ -31,6 +31,7 @@ typedef enum { struct form { char *action; /**< URL to submit to. */ form_method method; /**< Method and enctype. */ + char *charset; /**< Charset to submit form in */ struct form_control *controls; /**< Linked list of controls. */ struct form_control *last_control; /**< Last control in list. */ struct form *prev; /**< Previous form in doc. */ @@ -101,7 +102,7 @@ struct form_successful_control { struct form_successful_control *next; /**< Next in linked list. */ }; -struct form *form_new(char *action, form_method method); +struct form *form_new(char *action, form_method method, char *charset); struct form_control *form_new_control(form_control_type type); void form_add_control(struct form *form, struct form_control *control); void form_free_control(struct form_control *control); @@ -110,7 +111,8 @@ bool form_add_option(struct form_control *control, char *value, char *text, bool form_successful_controls(struct form *form, struct form_control *submit_button, struct form_successful_control **successful_controls); -char *form_url_encode(struct form_successful_control *control); +char *form_url_encode(struct form *form, + struct form_successful_control *control); void form_free_successful(struct form_successful_control *control); #endif |