2 wmget - A background download manager as a Window Maker dock app
3 Copyright (c) 2001-2003 Aaron Trickey <aaron@amtrickey.net>
5 Permission is hereby granted, free of charge, to any person
6 obtaining a copy of this software and associated documentation files
7 (the "Software"), to deal in the Software without restriction,
8 including without limitation the rights to use, copy, modify, merge,
9 publish, distribute, sublicense, and/or sell copies of the Software,
10 and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 ********************************************************************
25 request.c - Client code for submitting download requests
27 Invoked whenever wmget is run in "request" mode; submits a job to
40 static char *enquote_strdup (const char *string
)
42 int len
= strlen (string
);
47 for (src
= string
; *src
; src
+= strcspn (src
, ")\\")) {
51 dest
= newstr
= malloc (len
+ 1);
53 for (src
= string
; *src
; ++src
) {
69 int add_arg_s (char *line
, const char *argname
, const char *argval
)
73 if (strlen (line
) + strlen (argname
) + strlen (argval
) + 3
75 error ("Too much data to send to server!");
79 qargval
= enquote_strdup (argval
);
81 line
+= strlen (line
);
82 sprintf (line
, " %s(%s)", argname
, qargval
);
90 int add_arg_i (char *line
, const char *argname
, int argval
)
92 if (strlen (line
) + strlen (argname
) + 10
94 error ("Too much data to send to server!");
98 line
+= strlen (line
);
99 sprintf (line
, " %s(%d)", argname
, argval
);
105 int request (int argc
, char **argv
)
107 char line
[MAXCMDLEN
+ 1];
112 config_request (argc
, argv
, &req
);
114 if (!req
.source_url
) {
115 error ("Missing source URL!");
120 strcpy (line
, CMD_GET
);
121 if (add_arg_s (line
, ARG_GET_SOURCE_URL
, req
.source_url
))
125 if (add_arg_s (line
, ARG_GET_DISPLAY
, req
.display
))
129 if (add_arg_s (line
, ARG_GET_SAVE_TO
, req
.save_to
))
132 if (req
.overwrite
!= -1)
133 if (add_arg_i (line
, ARG_GET_OVERWRITE
, req
.overwrite
))
136 if (req
.continue_from
!= -1)
137 if (add_arg_i (line
, ARG_GET_CONTINUE_FROM
, req
.continue_from
))
141 if (add_arg_s (line
, ARG_GET_PROXY
, req
.proxy
))
144 if (req
.follow
!= -1)
145 if (add_arg_i (line
, ARG_GET_FOLLOW
, req
.follow
))
149 if (add_arg_s (line
, ARG_GET_UA
, req
.user_agent
))
152 if (req
.use_ascii
!= -1)
153 if (add_arg_i (line
, ARG_GET_USE_ASCII
, req
.use_ascii
))
157 if (add_arg_s (line
, ARG_GET_REFERER
, req
.referer
))
160 if (req
.include
!= -1)
161 if (add_arg_i (line
, ARG_GET_INCLUDE
, req
.include
))
165 if (add_arg_s (line
, ARG_GET_INTERFACE
, req
.interface
))
169 if (add_arg_s (line
, ARG_GET_PROXY_AUTH
, req
.proxy_auth
))
173 if (add_arg_s (line
, ARG_GET_AUTH
, req
.auth
))
176 debug ("Command line is '%s'", line
);
180 if (!(fp
= iq_client_connect ()))
183 if (fputs (line
, fp
) == EOF
) {
184 error_sys ("Failed to send command to server (fputs)");
189 if (!fgets (line
, sizeof line
- 1, fp
)) {
190 error ("Server did not respond to command!");
195 /* Extract the first word and compare. */
196 word_break
= line
+ strcspn (line
, " \t\r\n");
198 while (*word_break
&& isspace (*word_break
))
201 if (strcasecmp (line
, RESPONSE_JOB_ACCEPTED
)) {
202 error ("%s", word_break
);
209 /* Since we got a RESPONSE_JOB_ACCEPTED, present the user with the
210 * job ID for future cancellation.
212 printf ("Job ID is %s\n", word_break
);