2003-10-25 04:35:49 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
2004-08-08 23:13:40 +04:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2003-10-25 04:35:49 +04:00
|
|
|
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* Form handling functions (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2005-05-11 06:03:15 +04:00
|
|
|
#include <stdbool.h>
|
2004-03-21 23:07:14 +03:00
|
|
|
#include <stdio.h>
|
2003-10-25 04:35:49 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include "curl/curl.h"
|
2003-10-26 01:51:15 +04:00
|
|
|
#include "netsurf/render/box.h"
|
2003-10-25 04:35:49 +04:00
|
|
|
#include "netsurf/render/form.h"
|
2004-08-18 05:36:39 +04:00
|
|
|
#include "netsurf/utils/log.h"
|
2005-04-16 09:09:33 +04:00
|
|
|
#include "netsurf/utils/utf8.h"
|
2003-10-25 04:35:49 +04:00
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
|
2003-10-26 01:51:15 +04:00
|
|
|
static char *form_textarea_value(struct form_control *textarea);
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
/**
|
|
|
|
* Create a struct form.
|
|
|
|
*
|
|
|
|
* \param action URL to submit form to, used directly (not copied)
|
|
|
|
* \param method method and enctype
|
2005-04-16 09:09:33 +04:00
|
|
|
* \param charset characterset of form (not copied)
|
2004-12-13 00:51:01 +03:00
|
|
|
* \return a new structure, or 0 on memory exhaustion
|
|
|
|
*/
|
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
struct form *form_new(char *action, form_method method, char *charset)
|
2004-12-13 00:51:01 +03:00
|
|
|
{
|
|
|
|
struct form *form;
|
|
|
|
|
|
|
|
form = malloc(sizeof *form);
|
|
|
|
if (!form)
|
|
|
|
return 0;
|
|
|
|
form->action = action;
|
|
|
|
form->method = method;
|
2005-04-16 09:09:33 +04:00
|
|
|
form->charset = charset;
|
2004-12-13 00:51:01 +03:00
|
|
|
form->controls = 0;
|
|
|
|
form->last_control = 0;
|
2005-04-09 13:47:37 +04:00
|
|
|
form->prev = 0;
|
2004-12-13 00:51:01 +03:00
|
|
|
return form;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
/**
|
|
|
|
* Create a struct form_control.
|
|
|
|
*
|
|
|
|
* \param type control type
|
|
|
|
* \return a new structure, or 0 on memory exhaustion
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct form_control *form_new_control(form_control_type type)
|
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
if ((control = malloc(sizeof *control)) == NULL)
|
|
|
|
return NULL;
|
2004-05-21 14:25:42 +04:00
|
|
|
control->type = type;
|
2004-07-20 00:40:11 +04:00
|
|
|
control->name = NULL;
|
|
|
|
control->value = NULL;
|
|
|
|
control->initial_value = NULL;
|
2004-05-21 14:25:42 +04:00
|
|
|
control->disabled = false;
|
2004-07-20 00:40:11 +04:00
|
|
|
control->form = NULL;
|
|
|
|
control->box = NULL;
|
|
|
|
control->caret_inline_container = NULL;
|
|
|
|
control->caret_text_box = NULL;
|
|
|
|
control->caret_box_offset = control->caret_form_offset = 0;
|
|
|
|
control->length = control->maxlength = 0;
|
2004-05-22 03:42:26 +04:00
|
|
|
control->selected = false;
|
2004-07-20 00:40:11 +04:00
|
|
|
control->prev = NULL;
|
|
|
|
control->next = NULL;
|
2004-05-21 14:25:42 +04:00
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Add a control to the list of controls in a form.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void form_add_control(struct form *form, struct form_control *control)
|
|
|
|
{
|
|
|
|
control->form = form;
|
2004-07-20 00:40:11 +04:00
|
|
|
if (form->controls != NULL) {
|
2003-10-25 04:35:49 +04:00
|
|
|
assert(form->last_control);
|
|
|
|
form->last_control->next = control;
|
2004-04-09 03:46:41 +04:00
|
|
|
control->prev = form->last_control;
|
2004-07-20 00:40:11 +04:00
|
|
|
control->next = NULL;
|
2003-10-25 04:35:49 +04:00
|
|
|
form->last_control = control;
|
|
|
|
} else {
|
|
|
|
form->controls = form->last_control = control;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-21 14:25:42 +04:00
|
|
|
/**
|
|
|
|
* Free a struct form_control.
|
|
|
|
*
|
|
|
|
* \param control structure to free
|
|
|
|
*/
|
|
|
|
|
|
|
|
void form_free_control(struct form_control *control)
|
|
|
|
{
|
|
|
|
free(control->name);
|
|
|
|
free(control->value);
|
|
|
|
free(control->initial_value);
|
|
|
|
if (control->type == GADGET_SELECT) {
|
|
|
|
struct form_option *option, *next;
|
|
|
|
for (option = control->data.select.items; option;
|
|
|
|
option = next) {
|
|
|
|
next = option->next;
|
|
|
|
free(option->text);
|
|
|
|
free(option->value);
|
|
|
|
free(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(control);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-13 00:51:01 +03:00
|
|
|
/**
|
|
|
|
* Add an option to a form select control.
|
|
|
|
*
|
|
|
|
* \param control form control of type GADGET_SELECT
|
|
|
|
* \param value value of option, used directly (not copied)
|
|
|
|
* \param text text for option, used directly (not copied)
|
|
|
|
* \param selected this option is selected
|
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool form_add_option(struct form_control *control, char *value, char *text,
|
|
|
|
bool selected)
|
|
|
|
{
|
|
|
|
struct form_option *option;
|
|
|
|
|
|
|
|
assert(control);
|
|
|
|
assert(control->type == GADGET_SELECT);
|
|
|
|
|
|
|
|
option = malloc(sizeof *option);
|
|
|
|
if (!option)
|
|
|
|
return false;
|
|
|
|
option->selected = option->initial_selected = false;
|
|
|
|
option->value = value;
|
|
|
|
option->text = text;
|
|
|
|
option->next = 0;
|
|
|
|
|
|
|
|
/* add to linked list */
|
|
|
|
if (control->data.select.items == 0)
|
|
|
|
control->data.select.items = option;
|
|
|
|
else
|
|
|
|
control->data.select.last_item->next = option;
|
|
|
|
control->data.select.last_item = option;
|
|
|
|
|
|
|
|
/* set selected */
|
|
|
|
if (selected && (control->data.select.num_selected == 0 ||
|
|
|
|
control->data.select.multiple)) {
|
|
|
|
option->selected = option->initial_selected = true;
|
|
|
|
control->data.select.num_selected++;
|
|
|
|
control->data.select.current = option;
|
|
|
|
}
|
|
|
|
|
|
|
|
control->data.select.num_items++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Identify 'successful' controls.
|
|
|
|
*
|
2004-08-08 23:13:40 +04:00
|
|
|
* \param form form to search for successful controls
|
|
|
|
* \param submit_button control used to submit the form, if any
|
|
|
|
* \parma successful_controls updated to point to linked list of
|
|
|
|
* form_successful_control, 0 if no controls
|
|
|
|
* \return true on success, false on memory exhaustion
|
|
|
|
*
|
2003-10-25 04:35:49 +04:00
|
|
|
* See HTML 4.01 section 17.13.2.
|
|
|
|
*/
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
bool form_successful_controls(struct form *form,
|
|
|
|
struct form_control *submit_button,
|
|
|
|
struct form_successful_control **successful_controls)
|
2003-10-25 04:35:49 +04:00
|
|
|
{
|
|
|
|
struct form_control *control;
|
|
|
|
struct form_option *option;
|
2004-08-08 23:13:40 +04:00
|
|
|
struct form_successful_control sentinel, *last_success, *success_new;
|
|
|
|
char *value;
|
2005-05-11 06:03:15 +04:00
|
|
|
bool had_submit = false;
|
2004-07-20 00:40:11 +04:00
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
last_success = &sentinel;
|
2004-04-01 22:29:42 +04:00
|
|
|
sentinel.next = 0;
|
2003-10-25 04:35:49 +04:00
|
|
|
|
|
|
|
for (control = form->controls; control; control = control->next) {
|
|
|
|
/* ignore disabled controls */
|
|
|
|
if (control->disabled)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ignore controls with no name */
|
|
|
|
if (!control->name)
|
|
|
|
continue;
|
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
switch (control->type) {
|
|
|
|
case GADGET_HIDDEN:
|
2004-08-18 05:45:29 +04:00
|
|
|
/* ignore hidden controls with no value */
|
|
|
|
/* this fixes ebay silliness */
|
|
|
|
if (!control->value)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* fall through */
|
2004-07-20 00:40:11 +04:00
|
|
|
case GADGET_TEXTBOX:
|
|
|
|
case GADGET_PASSWORD:
|
2004-08-08 23:13:40 +04:00
|
|
|
value = strdup(control->value);
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
|
|
|
LOG(("failed to duplicate value"
|
|
|
|
"'%s' for control %s",
|
|
|
|
control->value,
|
|
|
|
control->name));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_RADIO:
|
|
|
|
case GADGET_CHECKBOX:
|
|
|
|
/* ignore checkboxes and radio buttons which
|
2004-08-08 23:13:40 +04:00
|
|
|
* aren't selected */
|
|
|
|
if (!control->selected)
|
|
|
|
continue;
|
2005-05-11 05:34:41 +04:00
|
|
|
if (control->value)
|
|
|
|
value = strdup(control->value);
|
|
|
|
else
|
|
|
|
value = strdup("on");
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
2005-05-11 05:34:41 +04:00
|
|
|
LOG(("failed to duplicate"
|
|
|
|
"value '%s' for"
|
|
|
|
"control %s",
|
|
|
|
control->value,
|
|
|
|
control->name));
|
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_SELECT:
|
|
|
|
/* select */
|
|
|
|
for (option = control->data.select.items;
|
|
|
|
option != NULL;
|
|
|
|
option = option->next) {
|
2004-08-08 23:13:40 +04:00
|
|
|
if (!option->selected)
|
|
|
|
continue;
|
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->file = false;
|
|
|
|
success_new->name = strdup(control->name);
|
|
|
|
success_new->value = strdup(option->value);
|
|
|
|
success_new->next = NULL;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
|
|
|
if (!success_new->name ||
|
2004-08-18 05:36:39 +04:00
|
|
|
!success_new->value) {
|
|
|
|
LOG(("strdup failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_TEXTAREA:
|
|
|
|
/* textarea */
|
2004-08-08 23:13:40 +04:00
|
|
|
value = form_textarea_value(control);
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
|
|
|
LOG(("failed handling textarea"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
if (value[0] == 0) {
|
|
|
|
free(value);
|
|
|
|
continue;
|
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_IMAGE: {
|
|
|
|
/* image */
|
|
|
|
const size_t len = strlen(control->name) + 3;
|
|
|
|
|
|
|
|
/* x */
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->file = false;
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->name = malloc(len);
|
|
|
|
success_new->value = malloc(20);
|
|
|
|
if (!success_new->name ||
|
|
|
|
!success_new->value) {
|
|
|
|
free(success_new->name);
|
|
|
|
free(success_new->value);
|
|
|
|
free(success_new);
|
2004-08-18 05:36:39 +04:00
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
sprintf(success_new->name, "%s.x",
|
|
|
|
control->name);
|
|
|
|
sprintf(success_new->value, "%i",
|
|
|
|
control->data.image.mx);
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->next = 0;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
|
|
|
|
|
|
|
/* y */
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->file = false;
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->name = malloc(len);
|
|
|
|
success_new->value = malloc(20);
|
|
|
|
if (!success_new->name ||
|
|
|
|
!success_new->value) {
|
|
|
|
free(success_new->name);
|
|
|
|
free(success_new->value);
|
|
|
|
free(success_new);
|
2004-08-18 05:36:39 +04:00
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
|
|
|
}
|
|
|
|
sprintf(success_new->name, "%s.y",
|
|
|
|
control->name);
|
|
|
|
sprintf(success_new->value, "%i",
|
|
|
|
control->data.image.my);
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->next = 0;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
}
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2004-07-20 00:40:11 +04:00
|
|
|
case GADGET_SUBMIT:
|
2005-05-11 06:03:15 +04:00
|
|
|
if (!submit_button && !had_submit)
|
|
|
|
/* no submit button specified, so
|
|
|
|
* use first declared in form */
|
|
|
|
had_submit = true;
|
|
|
|
else if (control != submit_button)
|
|
|
|
/* only the activated submit button
|
|
|
|
* is successful */
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
|
|
|
value = strdup(control->value);
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!value) {
|
|
|
|
LOG(("failed to duplicate value"
|
|
|
|
"'%s' for control %s",
|
|
|
|
control->value,
|
|
|
|
control->name));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_RESET:
|
|
|
|
/* ignore reset */
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GADGET_FILE:
|
|
|
|
/* file */
|
2004-08-08 23:13:40 +04:00
|
|
|
if (!control->value)
|
|
|
|
continue;
|
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->file = true;
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->name = strdup(control->name);
|
|
|
|
success_new->value = strdup(control->value);
|
2004-07-20 00:40:11 +04:00
|
|
|
success_new->next = 0;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new->name ||
|
|
|
|
!success_new->value) {
|
|
|
|
LOG(("strdup failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-07-20 00:40:11 +04:00
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
continue;
|
2004-07-20 00:40:11 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2004-03-21 23:07:14 +03:00
|
|
|
}
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new = malloc(sizeof(*success_new));
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new) {
|
|
|
|
LOG(("malloc failed"));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2004-08-08 23:13:40 +04:00
|
|
|
success_new->file = false;
|
|
|
|
success_new->name = strdup(control->name);
|
|
|
|
success_new->value = value;
|
|
|
|
success_new->next = NULL;
|
|
|
|
last_success->next = success_new;
|
|
|
|
last_success = success_new;
|
2004-08-18 05:36:39 +04:00
|
|
|
if (!success_new->name) {
|
|
|
|
LOG(("failed to duplicate name '%s'",
|
|
|
|
control->name));
|
2004-08-08 23:13:40 +04:00
|
|
|
goto no_memory;
|
2004-08-18 05:36:39 +04:00
|
|
|
}
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
*successful_controls = sentinel.next;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
no_memory:
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
form_free_successful(sentinel.next);
|
|
|
|
return false;
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-26 01:51:15 +04:00
|
|
|
/**
|
|
|
|
* Find the value for a textarea control.
|
2004-08-08 23:13:40 +04:00
|
|
|
*
|
|
|
|
* \param textarea control of type GADGET_TEXTAREA
|
|
|
|
* \return the value as a UTF-8 string on heap, or 0 on memory exhaustion
|
2003-10-26 01:51:15 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *form_textarea_value(struct form_control *textarea)
|
|
|
|
{
|
2004-08-08 23:13:40 +04:00
|
|
|
unsigned int len = 0;
|
2003-10-26 01:51:15 +04:00
|
|
|
char *value, *s;
|
2004-08-08 23:13:40 +04:00
|
|
|
struct box *text_box;
|
2003-10-26 01:51:15 +04:00
|
|
|
|
|
|
|
/* find required length */
|
2004-08-08 23:13:40 +04:00
|
|
|
for (text_box = textarea->box->children->children; text_box;
|
|
|
|
text_box = text_box->next) {
|
|
|
|
if (text_box->type == BOX_INLINE)
|
2003-10-26 01:51:15 +04:00
|
|
|
len += text_box->length + 1;
|
2004-08-08 23:13:40 +04:00
|
|
|
else /* BOX_BR */
|
|
|
|
len += 2;
|
2003-10-26 01:51:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* construct value */
|
2004-08-08 23:13:40 +04:00
|
|
|
s = value = malloc(len + 1);
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
for (text_box = textarea->box->children->children; text_box;
|
|
|
|
text_box = text_box->next) {
|
|
|
|
if (text_box->type == BOX_INLINE) {
|
2003-10-26 01:51:15 +04:00
|
|
|
strncpy(s, text_box->text, text_box->length);
|
|
|
|
s += text_box->length;
|
2005-04-16 23:34:30 +04:00
|
|
|
if (text_box->next)
|
|
|
|
/* only add space if this isn't the last box */
|
|
|
|
*s++ = ' ';
|
2004-08-08 23:13:40 +04:00
|
|
|
} else { /* BOX_BR */
|
|
|
|
*s++ = '\r';
|
|
|
|
*s++ = '\n';
|
2003-10-26 01:51:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
/**
|
|
|
|
* Encode controls using application/x-www-form-urlencoded.
|
2004-08-08 23:13:40 +04:00
|
|
|
*
|
2005-04-16 09:09:33 +04:00
|
|
|
* \param form form to which successful controls relate
|
2004-08-08 23:13:40 +04:00
|
|
|
* \param control linked list of form_successful_control
|
|
|
|
* \return URL-encoded form, or 0 on memory exhaustion
|
2003-10-25 04:35:49 +04:00
|
|
|
*/
|
|
|
|
|
2005-04-16 09:09:33 +04:00
|
|
|
char *form_url_encode(struct form *form,
|
|
|
|
struct form_successful_control *control)
|
2003-10-25 04:35:49 +04:00
|
|
|
{
|
2005-04-16 09:09:33 +04:00
|
|
|
char *name, *value, *n_temp, *v_temp;
|
2004-08-08 23:13:40 +04:00
|
|
|
char *s = malloc(1), *s2;
|
2003-10-26 01:51:15 +04:00
|
|
|
unsigned int len = 0, len1;
|
2003-10-25 04:35:49 +04:00
|
|
|
|
2004-08-08 23:13:40 +04:00
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
s[0] = 0;
|
|
|
|
|
2003-10-25 04:35:49 +04:00
|
|
|
for (; control; control = control->next) {
|
2005-04-16 09:09:33 +04:00
|
|
|
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);
|
2003-10-25 04:35:49 +04:00
|
|
|
len1 = len + strlen(name) + strlen(value) + 2;
|
2004-08-08 23:13:40 +04:00
|
|
|
s2 = realloc(s, len1 + 1);
|
|
|
|
if (!s2) {
|
2005-04-16 09:09:33 +04:00
|
|
|
curl_free(value);
|
|
|
|
curl_free(name);
|
|
|
|
free(v_temp);
|
|
|
|
free(n_temp);
|
2004-08-08 23:13:40 +04:00
|
|
|
free(s);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s = s2;
|
2003-10-25 04:35:49 +04:00
|
|
|
sprintf(s + len, "%s=%s&", name, value);
|
|
|
|
len = len1;
|
2004-07-20 00:40:11 +04:00
|
|
|
curl_free(name);
|
|
|
|
curl_free(value);
|
2005-04-16 09:09:33 +04:00
|
|
|
free(v_temp);
|
|
|
|
free(n_temp);
|
2003-10-25 04:35:49 +04:00
|
|
|
}
|
|
|
|
if (len)
|
|
|
|
s[len - 1] = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free a linked list of form_successful_control.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void form_free_successful(struct form_successful_control *control)
|
|
|
|
{
|
|
|
|
struct form_successful_control *next;
|
|
|
|
for (; control; control = next) {
|
|
|
|
next = control->next;
|
|
|
|
free(control->name);
|
|
|
|
free(control->value);
|
|
|
|
free(control);
|
|
|
|
}
|
|
|
|
}
|