[project @ 2003-05-22 13:21:45 by bursa]

Form GET support (John M Bell)

svn path=/import/netsurf/; revision=130
This commit is contained in:
James Bursa 2003-05-22 13:21:45 +00:00
parent 27d93c182c
commit 80fe931f80
5 changed files with 170 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/** /**
* $Id: browser.c,v 1.36 2003/05/10 11:13:34 bursa Exp $ * $Id: browser.c,v 1.37 2003/05/22 13:21:45 bursa Exp $
*/ */
#include "netsurf/content/cache.h" #include "netsurf/content/cache.h"
@ -11,6 +11,7 @@
#include "netsurf/utils/utils.h" #include "netsurf/utils/utils.h"
#include "libxml/uri.h" #include "libxml/uri.h"
#include "libxml/debugXML.h" #include "libxml/debugXML.h"
#include "curl/curl.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@ -36,6 +37,9 @@ static void gui_redraw_gadget2(struct browser_window* bw, struct box* box, struc
unsigned long x, unsigned long y); unsigned long x, unsigned long y);
static void browser_window_gadget_select(struct browser_window* bw, struct gui_gadget* g, int item); static void browser_window_gadget_select(struct browser_window* bw, struct gui_gadget* g, int item);
static int browser_window_gadget_click(struct browser_window* bw, unsigned long click_x, unsigned long click_y); static int browser_window_gadget_click(struct browser_window* bw, unsigned long click_x, unsigned long click_y);
static void browser_form_submit(struct browser_window *bw, struct form *form);
static char* browser_form_construct_get(struct page_elements *elements, struct formsubmit* fs);
static void browser_form_get_append(char **s, int *length, char sep, char *name, char *value);
void browser_window_start_throbber(struct browser_window* bw) void browser_window_start_throbber(struct browser_window* bw)
@ -418,8 +422,11 @@ int browser_window_gadget_click(struct browser_window* bw, unsigned long click_x
gui_redraw_gadget(bw, g); gui_redraw_gadget(bw, g);
break; break;
case GADGET_ACTIONBUTTON: case GADGET_ACTIONBUTTON:
/* redraw button */
g->data.actionbutt.pressed = -1; g->data.actionbutt.pressed = -1;
gui_redraw_gadget(bw, g); gui_redraw_gadget(bw, g);
if (stricmp(g->data.actionbutt.butttype,"submit") == 0)
browser_form_submit(bw, g->form);
break; break;
case GADGET_TEXTAREA: case GADGET_TEXTAREA:
gui_edit_textarea(bw, g); gui_edit_textarea(bw, g);
@ -866,3 +873,115 @@ void browser_window_redraw_boxes(struct browser_window* bw, struct box_position*
} }
void browser_form_submit(struct browser_window *bw, struct form *form)
{
/*create submission request*/
struct formsubmit* fs = (struct formsubmit*) xcalloc(1, sizeof(struct formsubmit));
fs->form = form;
/*fs->items = g;*/
LOG(("Submission request created"));
if (fs->form->method == method_GET) {
/*GET request*/
/*GET basically munges the entire form data
into one URL. */
char *url = browser_form_construct_get(&bw->current_content->data.html.elements,
fs);
LOG(("GET request"));
/*send request*/
browser_window_open_location(bw, url);
xfree(url);
} else {
/*POST request*/
assert(fs->form->method == method_POST);
LOG(("POST request - not implemented yet"));
/*POST is a standard HTTP method.
Basically, it creates a new request
and sends the form data as the request
body.*/
}
xfree(fs);
}
char* browser_form_construct_get(struct page_elements *elements, struct formsubmit* fs)
{
char *ret;
int i,j, length;
struct formoption* opt;
ret = xstrdup(fs->form->action);
length = strlen(ret);
j=0;
for (i=0;i<elements->numGadgets;i++){
if(elements->gadgets[i]->form == fs->form){
if(elements->gadgets[i]->name != 0){
char *value = 0;
switch(elements->gadgets[i]->type){
case GADGET_HIDDEN: value = elements->gadgets[i]->data.hidden.value;
break;
case GADGET_TEXTBOX: value = elements->gadgets[i]->data.textbox.text;
break;
case GADGET_RADIO: if(elements->gadgets[i]->data.radio.selected == -1)
value = elements->gadgets[i]->data.radio.value;
break;
case GADGET_CHECKBOX: if(elements->gadgets[i]->data.checkbox.selected == 1)
value = elements->gadgets[i]->data.checkbox.value;
break;
case GADGET_SELECT: opt = elements->gadgets[i]->data.select.items;
while(opt != NULL){
if(opt->selected == -1 || opt->selected == 1) {
browser_form_get_append(&ret, &length, j == 0 ? '?' : '&',
elements->gadgets[i]->name, opt->value);
j++;
}
opt = opt->next;
}
break;
case GADGET_TEXTAREA: value = elements->gadgets[i]->data.textarea.text;
break;
default: break;
}
if (value != 0) {
browser_form_get_append(&ret, &length, j == 0 ? '?' : '&',
elements->gadgets[i]->name, value);
j++;
}
}
}
}
return ret;
}
void browser_form_get_append(char **s, int *length, char sep, char *name, char *value)
{
unsigned int length1;
name = curl_escape(name, 0);
value = curl_escape(value, 0);
length1 = 2 + strlen(name) + strlen(value);
LOG(("append %c%s=%s, length1 %i, *s %p", sep, name, value, length1, *s));
*s = xrealloc(*s, *length + length1 + 1);
sprintf(*s + *length, "%c%s=%s", sep, name, value);
*length += length1;
curl_free(name);
curl_free(value);
}

View File

@ -103,6 +103,7 @@ http://netsurf.strcprstskrzkrk.co.uk/developer/
libxml (XML and HTML parser) http://xmlsoft.org/ libxml (XML and HTML parser) http://xmlsoft.org/
libcurl (HTTP, FTP, etc) http://curl.haxx.se/libcurl/ libcurl (HTTP, FTP, etc) http://curl.haxx.se/libcurl/
OSLib (C interface to RISC OS SWIs) http://ro-oslib.sourceforge.net/ OSLib (C interface to RISC OS SWIs) http://ro-oslib.sourceforge.net/
libutf-8 http://www.whizkidtech.redprince.net/i18n/ libpng (PNG support) http://www.libpng.org/pub/png/libpng.html
zlib http://www.gzip.org/zlib/
________________________________________________________________________________ ________________________________________________________________________________

View File

@ -1,5 +1,5 @@
/** /**
* $Id: box.c,v 1.43 2003/04/25 08:03:15 bursa Exp $ * $Id: box.c,v 1.44 2003/05/22 13:21:45 bursa Exp $
*/ */
#include <assert.h> #include <assert.h>
@ -1270,6 +1270,9 @@ struct box* box_input(xmlNode * n, struct css_style* style, struct form* current
if ((s = (char *) xmlGetProp(n, (const xmlChar *) "name"))) { if ((s = (char *) xmlGetProp(n, (const xmlChar *) "name"))) {
box->gadget->name = s; box->gadget->name = s;
} }
box->gadget->data.actionbutt.butttype = strdup(type);
add_gadget_element(elements, box->gadget); add_gadget_element(elements, box->gadget);
} }

View File

@ -1,5 +1,5 @@
/** /**
* $Id: box.h,v 1.24 2003/04/15 17:53:00 bursa Exp $ * $Id: box.h,v 1.25 2003/05/22 13:21:45 bursa Exp $
*/ */
#ifndef _NETSURF_RENDER_BOX_H_ #ifndef _NETSURF_RENDER_BOX_H_
@ -49,6 +49,7 @@ struct gui_gadget {
int size; int size;
} textbox; } textbox;
struct { struct {
char* butttype;
char* label; char* label;
int pressed; int pressed;
} actionbutt; } actionbutt;

View File

@ -1,5 +1,5 @@
/** /**
* $Id: utils.c,v 1.8 2003/04/11 21:06:51 bursa Exp $ * $Id: utils.c,v 1.9 2003/05/22 13:21:45 bursa Exp $
*/ */
#include <ctype.h> #include <ctype.h>
@ -135,11 +135,48 @@ char *squash_tolat1(xmlChar *s)
char *url_join(const char* new, const char* base) char *url_join(const char* new, const char* base)
{ {
char* ret; char* ret, *nn;
int i; int i,j,k;
LOG(("new = %s, base = %s", new, base)); LOG(("new = %s, base = %s", new, base));
/* deal with spaces and quotation marks in URLs etc.
also removes spaces from end of links.
There's definitely a better way to do this */
nn = xcalloc(strlen(new) * 3 + 40, sizeof(char));
j=0;
for(i=0;i<strlen(new);i++){
if(new[i] == ' '){ /* space */
nn[j] = '%';
nn[j+1] = '2';
nn[j+2] = '0';
j+=2;
}
else if(new[i] == '"'){ /* quotes */
nn[j] = '%';
nn[j+1] = '2';
nn[j+2] = '2';
j+=2;
k = j;
}
else{
nn[j] = new[i];
k = j;
}
j++;
}
if(k < j){
nn[k+1] = '\0';
LOG(("before: %s after: %s", new, nn));
}
new = nn;
if (base == 0) if (base == 0)
{ {
/* no base, so make an absolute URL */ /* no base, so make an absolute URL */
@ -184,6 +221,8 @@ char *url_join(const char* new, const char* base)
ret = xcalloc(strlen(new) + 10, sizeof(char)); ret = xcalloc(strlen(new) + 10, sizeof(char));
strcpy(ret, new); strcpy(ret, new);
} }
xfree(nn);
return ret; return ret;
} }