Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / dict.c
blobdc4f5a46774a916677f53ed7eb579c470716fa49
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: dict.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #ifndef CURL_DISABLE_DICT
28 /* -- WIN32 approved -- */
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <ctype.h>
35 #ifdef WIN32
36 #include <time.h>
37 #include <io.h>
38 #else
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #include <netinet/in.h>
43 #ifdef HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #include <netdb.h>
50 #ifdef HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
52 #endif
53 #ifdef HAVE_NET_IF_H
54 #include <net/if.h>
55 #endif
56 #include <sys/ioctl.h>
57 #include <signal.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
61 #endif
63 #ifdef HAVE_SYS_SELECT_H
64 #include <sys/select.h>
65 #endif
68 #endif
70 #include "urldata.h"
71 #include <curl/curl.h>
72 #include "transfer.h"
73 #include "sendf.h"
75 #include "progress.h"
76 #include "strequal.h"
77 #include "dict.h"
79 #define _MPRINTF_REPLACE /* use our functions only */
80 #include <curl/mprintf.h>
82 /* The last #include file should be: */
83 #include "memdebug.h"
87 * Forward declarations.
90 static CURLcode dict_do(struct connectdata *conn, bool *done);
93 * DICT protocol handler.
96 const struct Curl_handler Curl_handler_dict = {
97 "DICT", /* scheme */
98 ZERO_NULL, /* setup_connection */
99 dict_do, /* do_it */
100 ZERO_NULL, /* done */
101 ZERO_NULL, /* do_more */
102 ZERO_NULL, /* connect_it */
103 ZERO_NULL, /* connecting */
104 ZERO_NULL, /* doing */
105 ZERO_NULL, /* proto_getsock */
106 ZERO_NULL, /* doing_getsock */
107 ZERO_NULL, /* disconnect */
108 PORT_DICT, /* defport */
109 PROT_DICT /* protocol */
112 static char *unescape_word(struct SessionHandle *data, const char *inp)
114 char *newp;
115 char *dictp;
116 char *ptr;
117 int len;
118 char byte;
119 int olen=0;
121 newp = curl_easy_unescape(data, inp, 0, &len);
122 if(!newp)
123 return NULL;
125 dictp = malloc(len*2 + 1); /* add one for terminating zero */
126 if(dictp) {
127 /* According to RFC2229 section 2.2, these letters need to be escaped with
128 \[letter] */
129 for(ptr = newp;
130 (byte = *ptr) != 0;
131 ptr++) {
132 if((byte <= 32) || (byte == 127) ||
133 (byte == '\'') || (byte == '\"') || (byte == '\\')) {
134 dictp[olen++] = '\\';
136 dictp[olen++] = byte;
138 dictp[olen]=0;
140 free(newp);
142 return dictp;
145 static CURLcode dict_do(struct connectdata *conn, bool *done)
147 char *word;
148 char *eword;
149 char *ppath;
150 char *database = NULL;
151 char *strategy = NULL;
152 char *nthdef = NULL; /* This is not part of the protocol, but required
153 by RFC 2229 */
154 CURLcode result=CURLE_OK;
155 struct SessionHandle *data=conn->data;
156 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
158 char *path = data->state.path;
159 curl_off_t *bytecount = &data->req.bytecount;
161 *done = TRUE; /* unconditionally */
163 if(conn->bits.user_passwd) {
164 /* AUTH is missing */
167 if(strnequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
168 strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
169 strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
171 word = strchr(path, ':');
172 if(word) {
173 word++;
174 database = strchr(word, ':');
175 if(database) {
176 *database++ = (char)0;
177 strategy = strchr(database, ':');
178 if(strategy) {
179 *strategy++ = (char)0;
180 nthdef = strchr(strategy, ':');
181 if(nthdef) {
182 *nthdef++ = (char)0;
188 if((word == NULL) || (*word == (char)0)) {
189 infof(data, "lookup word is missing");
190 word=(char *)"default";
192 if((database == NULL) || (*database == (char)0)) {
193 database = (char *)"!";
195 if((strategy == NULL) || (*strategy == (char)0)) {
196 strategy = (char *)".";
199 eword = unescape_word(data, word);
200 if(!eword)
201 return CURLE_OUT_OF_MEMORY;
203 result = Curl_sendf(sockfd, conn,
204 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
205 "MATCH "
206 "%s " /* database */
207 "%s " /* strategy */
208 "%s\r\n" /* word */
209 "QUIT\r\n",
211 database,
212 strategy,
213 eword
216 free(eword);
218 if(result)
219 failf(data, "Failed sending DICT request");
220 else
221 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
222 -1, NULL); /* no upload */
223 if(result)
224 return result;
226 else if(strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
227 strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
228 strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
230 word = strchr(path, ':');
231 if(word) {
232 word++;
233 database = strchr(word, ':');
234 if(database) {
235 *database++ = (char)0;
236 nthdef = strchr(database, ':');
237 if(nthdef) {
238 *nthdef++ = (char)0;
243 if((word == NULL) || (*word == (char)0)) {
244 infof(data, "lookup word is missing");
245 word=(char *)"default";
247 if((database == NULL) || (*database == (char)0)) {
248 database = (char *)"!";
251 eword = unescape_word(data, word);
252 if(!eword)
253 return CURLE_OUT_OF_MEMORY;
255 result = Curl_sendf(sockfd, conn,
256 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
257 "DEFINE "
258 "%s " /* database */
259 "%s\r\n" /* word */
260 "QUIT\r\n",
261 database,
262 eword);
264 free(eword);
266 if(result)
267 failf(data, "Failed sending DICT request");
268 else
269 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
270 -1, NULL); /* no upload */
272 if(result)
273 return result;
276 else {
278 ppath = strchr(path, '/');
279 if(ppath) {
280 int i;
282 ppath++;
283 for (i = 0; ppath[i]; i++) {
284 if(ppath[i] == ':')
285 ppath[i] = ' ';
287 result = Curl_sendf(sockfd, conn,
288 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
289 "%s\r\n"
290 "QUIT\r\n", ppath);
291 if(result)
292 failf(data, "Failed sending DICT request");
293 else
294 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
295 -1, NULL);
296 if(result)
297 return result;
301 return CURLE_OK;
303 #endif /*CURL_DISABLE_DICT*/