Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / dict.c
blobda5575daf0e2976aa2bd1d56e8445861cd934cba
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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.2 2007/03/15 19:22:13 andy 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>
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #ifdef HAVE_SYS_STAT_H
38 #include <sys/stat.h>
39 #endif
41 #ifdef WIN32
42 #include <time.h>
43 #include <io.h>
44 #else
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 #include <netinet/in.h>
49 #ifdef HAVE_SYS_TIME_H
50 #include <sys/time.h>
51 #endif
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 #include <netdb.h>
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59 #ifdef HAVE_NET_IF_H
60 #include <net/if.h>
61 #endif
62 #include <sys/ioctl.h>
63 #include <signal.h>
65 #ifdef HAVE_SYS_PARAM_H
66 #include <sys/param.h>
67 #endif
69 #ifdef HAVE_SYS_SELECT_H
70 #include <sys/select.h>
71 #endif
74 #endif
76 #include "urldata.h"
77 #include <curl/curl.h>
78 #include "transfer.h"
79 #include "sendf.h"
81 #include "progress.h"
82 #include "strequal.h"
83 #include "dict.h"
85 #define _MPRINTF_REPLACE /* use our functions only */
86 #include <curl/mprintf.h>
88 /* The last #include file should be: */
89 #include "memdebug.h"
91 static char *unescape_word(struct SessionHandle *data, char *inp)
93 char *newp;
94 char *dictp;
95 char *ptr;
96 int len;
97 unsigned char byte;
98 int olen=0;
100 newp = curl_easy_unescape(data, inp, 0, &len);
101 if(!newp)
102 return NULL;
104 dictp = malloc(len*2 + 1); /* add one for terminating zero */
105 if(dictp) {
106 /* According to RFC2229 section 2.2, these letters need to be escaped with
107 \[letter] */
108 for(ptr = newp;
109 (byte = (unsigned char)*ptr) != 0;
110 ptr++) {
111 if ((byte <= 32) || (byte == 127) ||
112 (byte == '\'') || (byte == '\"') || (byte == '\\')) {
113 dictp[olen++] = '\\';
115 dictp[olen++] = byte;
117 dictp[olen]=0;
119 free(newp);
121 return dictp;
124 CURLcode Curl_dict(struct connectdata *conn, bool *done)
126 char *word;
127 char *eword;
128 char *ppath;
129 char *database = NULL;
130 char *strategy = NULL;
131 char *nthdef = NULL; /* This is not part of the protocol, but required
132 by RFC 2229 */
133 CURLcode result=CURLE_OK;
134 struct SessionHandle *data=conn->data;
135 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
137 char *path = data->reqdata.path;
138 curl_off_t *bytecount = &data->reqdata.keep.bytecount;
140 *done = TRUE; /* unconditionally */
142 if(conn->bits.user_passwd) {
143 /* AUTH is missing */
146 if (strnequal(path, DICT_MATCH, sizeof(DICT_MATCH)-1) ||
147 strnequal(path, DICT_MATCH2, sizeof(DICT_MATCH2)-1) ||
148 strnequal(path, DICT_MATCH3, sizeof(DICT_MATCH3)-1)) {
150 word = strchr(path, ':');
151 if (word) {
152 word++;
153 database = strchr(word, ':');
154 if (database) {
155 *database++ = (char)0;
156 strategy = strchr(database, ':');
157 if (strategy) {
158 *strategy++ = (char)0;
159 nthdef = strchr(strategy, ':');
160 if (nthdef) {
161 *nthdef++ = (char)0;
167 if ((word == NULL) || (*word == (char)0)) {
168 failf(data, "lookup word is missing");
170 if ((database == NULL) || (*database == (char)0)) {
171 database = (char *)"!";
173 if ((strategy == NULL) || (*strategy == (char)0)) {
174 strategy = (char *)".";
177 eword = unescape_word(data, word);
178 if(!eword)
179 return CURLE_OUT_OF_MEMORY;
181 result = Curl_sendf(sockfd, conn,
182 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
183 "MATCH "
184 "%s " /* database */
185 "%s " /* strategy */
186 "%s\r\n" /* word */
187 "QUIT\r\n",
189 database,
190 strategy,
191 eword
194 free(eword);
196 if(result)
197 failf(data, "Failed sending DICT request");
198 else
199 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
200 -1, NULL); /* no upload */
201 if(result)
202 return result;
204 else if (strnequal(path, DICT_DEFINE, sizeof(DICT_DEFINE)-1) ||
205 strnequal(path, DICT_DEFINE2, sizeof(DICT_DEFINE2)-1) ||
206 strnequal(path, DICT_DEFINE3, sizeof(DICT_DEFINE3)-1)) {
208 word = strchr(path, ':');
209 if (word) {
210 word++;
211 database = strchr(word, ':');
212 if (database) {
213 *database++ = (char)0;
214 nthdef = strchr(database, ':');
215 if (nthdef) {
216 *nthdef++ = (char)0;
221 if ((word == NULL) || (*word == (char)0)) {
222 failf(data, "lookup word is missing");
224 if ((database == NULL) || (*database == (char)0)) {
225 database = (char *)"!";
228 eword = unescape_word(data, word);
229 if(!eword)
230 return CURLE_OUT_OF_MEMORY;
232 result = Curl_sendf(sockfd, conn,
233 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
234 "DEFINE "
235 "%s " /* database */
236 "%s\r\n" /* word */
237 "QUIT\r\n",
238 database,
239 eword);
241 free(eword);
243 if(result)
244 failf(data, "Failed sending DICT request");
245 else
246 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
247 -1, NULL); /* no upload */
249 if(result)
250 return result;
253 else {
255 ppath = strchr(path, '/');
256 if (ppath) {
257 int i;
259 ppath++;
260 for (i = 0; ppath[i]; i++) {
261 if (ppath[i] == ':')
262 ppath[i] = ' ';
264 result = Curl_sendf(sockfd, conn,
265 "CLIENT " LIBCURL_NAME " " LIBCURL_VERSION "\r\n"
266 "%s\r\n"
267 "QUIT\r\n", ppath);
268 if(result)
269 failf(data, "Failed sending DICT request");
270 else
271 result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, bytecount,
272 -1, NULL);
273 if(result)
274 return result;
278 return CURLE_OK;
280 #endif /*CURL_DISABLE_DICT*/