cURL / Mailing Lists / curl-users / Single Mail

curl-users

Re: [PATH] --data-urlencode

From: Alessandro Vesely <vesely_at_tana.it>
Date: Wed, 21 Nov 2007 09:47:22 +0100

Daniel Stenberg wrote:
> On Tue, 20 Nov 2007, Alessandro Vesely wrote:
>
> HTML pages with dynamic form input names are very rare so the need for
> dynamicly encodes of names should be much less frequent.

Even if they are static, they may be complicate, e.g. the Java-generated
stuff that Bart exemplified a couple of days ago.

>> However, the only way I've been able to view the encoded data is by
>> using -Gv or --trace options. It would be handy to know how to encode
>> a complicated name before resorting to an external website:
>
> I don't see why post data is any special here than all other kinds of
> operations you do with curl?

So that one can learn how to encode the static name in the script, even
if it's not curl's job. E.g.

D:\tmp\curl\curl\src>curl --data-urlencode "name=ctl00$MainContent$UpdatePanel1|
ctl00$MainContent$btnSearchDocType"
curl: no URL to post "name=ctl00%24MainContent%24UpdatePanel1%7Cctl00%24MainCont
ent%24btnSearchDocType"!
curl: try 'curl --help' or 'curl --manual' for more information

That patched program is just aesthetically horrible, as it performs the
intended function while pretending that it got a wrong syntax.

BTW, the syntax is slightly over-restrictive: --data-urlencode "stuff"
gives "option --data-urlencode: is badly used here", which looks rather
unnecessary. The CGI mandates a name=value syntax, but http accepts any
input. In addition, curl accepts --data-urlencode "=stuff", not good CGI.
I would patch it as attached, to urlencode any argument.

> I mean, you don't get to see a lot of stuff until you actually send it!

This is a possibly related, yet different problem. Usually, one does not
care to bombard a server with sending attempts (which may be good testing
anyway.) However, if one needs to be careful and wants to test a command
before issuing it, and does not have a test server at hand, there are not
many ways to proceed. The latter problem seems even more marginal than the
former. Perhaps using "file://-" for output could solve both of them?

Index: main.c
===================================================================
RCS file: /cvsroot/curl/curl/src/main.c,v
retrieving revision 1.431
diff -u -r1.431 main.c
--- main.c 20 Nov 2007 10:08:43 -0000 1.431
+++ main.c 21 Nov 2007 08:26:14 -0000
@@ -2059,19 +2059,21 @@
            */
           char *p = strchr(nextarg, '=');
           long size = 0;
- size_t nlen;
+ int nlen;
+ char is_file;
           if(!p)
             p = strchr(nextarg, '@');
- if(!p) {
- warnf(config, "bad use of --data-urlencode\n");
- return PARAM_BAD_USE;
+ if (p) {
+ nlen = p - nextarg; /* length of the name part */
+ is_file = *p++; /* pass the separator */
           }
- nlen = p - nextarg; /* length of the name part */
- if('@' == *p) {
+ else {
+ nlen = is_file = -1;
+ p = nextarg;
+ }
+ if('@' == is_file) {
             /* a '@' letter, it means that a file name or - (stdin) follows */
 
- p++; /* pass the separator */
-
             if(curlx_strequal("-", p)) {
               file = stdin;
               SET_BINMODE(stdin);
@@ -2090,7 +2092,7 @@
               fclose(file);
           }
           else {
- GetStr(&postdata, ++p);
+ GetStr(&postdata, p);
             size = strlen(postdata);
           }
 
@@ -2108,8 +2110,10 @@
               char *n = malloc(outlen);
               if(!n)
                 return PARAM_NO_MEM;
-
- snprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
+ if (nlen >= 0)
+ snprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
+ else
+ strcpy(n, enc);
               curl_free(enc);
               free(postdata);
               if(n) {
Received on 2007-11-21