~ubuntu-branches/ubuntu/dapper/curl/dapper

« back to all changes in this revision

Viewing changes to lib/sendf.c

  • Committer: Bazaar Package Importer
  • Author(s): Domenico Andreoli
  • Date: 2002-03-12 19:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20020312190621-iqx7k9cipo5d0ifr
Tags: upstream-7.9.5
ImportĀ upstreamĀ versionĀ 7.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 *                                  _   _ ____  _     
 
3
 *  Project                     ___| | | |  _ \| |    
 
4
 *                             / __| | | | |_) | |    
 
5
 *                            | (__| |_| |  _ <| |___ 
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 2002, Daniel Stenberg, <daniel@haxx.se>, et al.
 
9
 *
 
10
 * In order to be useful for every potential user, curl and libcurl are
 
11
 * dual-licensed under the MPL and the MIT/X-derivate licenses.
 
12
 *
 
13
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
14
 * copies of the Software, and permit persons to whom the Software is
 
15
 * furnished to do so, under the terms of the MPL or the MIT/X-derivate
 
16
 * licenses. You may pick one of these licenses.
 
17
 *
 
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 
19
 * KIND, either express or implied.
 
20
 *
 
21
 * $Id: sendf.c,v 1.44 2002/02/05 15:33:00 bagder Exp $
 
22
 *****************************************************************************/
 
23
 
 
24
#include "setup.h"
 
25
 
 
26
#include <stdio.h>
 
27
#include <stdarg.h>
 
28
#include <stdlib.h>
 
29
#include <errno.h>
 
30
 
 
31
#ifdef HAVE_SYS_TYPES_H
 
32
#include <sys/types.h>
 
33
#endif
 
34
 
 
35
#ifdef HAVE_SYS_SOCKET_H
 
36
#include <sys/socket.h> /* required for send() & recv() prototypes */
 
37
#endif
 
38
 
 
39
#ifdef HAVE_UNISTD_H
 
40
#include <unistd.h>
 
41
#endif
 
42
#if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
 
43
#include <winsock.h>
 
44
#endif
 
45
 
 
46
#include <curl/curl.h>
 
47
#include "urldata.h"
 
48
#include "sendf.h"
 
49
 
 
50
#define _MPRINTF_REPLACE /* use the internal *printf() functions */
 
51
#include <curl/mprintf.h>
 
52
 
 
53
#ifdef KRB4
 
54
#include "security.h"
 
55
#endif
 
56
#include <string.h>
 
57
/* The last #include file should be: */
 
58
#ifdef MALLOCDEBUG
 
59
#include "memdebug.h"
 
60
#endif
 
61
 
 
62
/* returns last node in linked list */
 
63
static struct curl_slist *slist_get_last(struct curl_slist *list)
 
64
{
 
65
        struct curl_slist       *item;
 
66
 
 
67
        /* if caller passed us a NULL, return now */
 
68
        if (!list)
 
69
                return NULL;
 
70
 
 
71
        /* loop through to find the last item */
 
72
        item = list;
 
73
        while (item->next) {
 
74
                item = item->next;
 
75
        }
 
76
        return item;
 
77
}
 
78
 
 
79
/* append a struct to the linked list. It always retunrs the address of the
 
80
 * first record, so that you can sure this function as an initialization
 
81
 * function as well as an append function. If you find this bothersome,
 
82
 * then simply create a separate _init function and call it appropriately from
 
83
 * within the proram. */
 
84
struct curl_slist *curl_slist_append(struct curl_slist *list,
 
85
                                     const char *data)
 
86
{
 
87
        struct curl_slist       *last;
 
88
        struct curl_slist       *new_item;
 
89
 
 
90
        new_item = (struct curl_slist *) malloc(sizeof(struct curl_slist));
 
91
        if (new_item) {
 
92
                new_item->next = NULL;
 
93
                new_item->data = strdup(data);
 
94
        }
 
95
        else {
 
96
                fprintf(stderr, "Cannot allocate memory for QUOTE list.\n");
 
97
                return NULL;
 
98
        }
 
99
 
 
100
        if (list) {
 
101
                last = slist_get_last(list);
 
102
                last->next = new_item;
 
103
                return list;
 
104
        }
 
105
 
 
106
        /* if this is the first item, then new_item *is* the list */
 
107
        return new_item;
 
108
}
 
109
 
 
110
/* be nice and clean up resources */
 
111
void curl_slist_free_all(struct curl_slist *list)
 
112
{
 
113
        struct curl_slist       *next;
 
114
        struct curl_slist       *item;
 
115
 
 
116
        if (!list)
 
117
                return;
 
118
 
 
119
        item = list;
 
120
        do {
 
121
                next = item->next;
 
122
                
 
123
                if (item->data) {
 
124
                        free(item->data);
 
125
                }
 
126
                free(item);
 
127
                item = next;
 
128
        } while (next);
 
129
}
 
130
 
 
131
 
 
132
/* Curl_infof() is for info message along the way */
 
133
 
 
134
void Curl_infof(struct SessionHandle *data, const char *fmt, ...)
 
135
{
 
136
  va_list ap;
 
137
  if(data->set.verbose) {
 
138
    va_start(ap, fmt);
 
139
    fputs("* ", data->set.err);
 
140
    vfprintf(data->set.err, fmt, ap);
 
141
    va_end(ap);
 
142
  }
 
143
}
 
144
 
 
145
/* Curl_failf() is for messages stating why we failed.
 
146
 * The message SHALL NOT include any LF or CR.
 
147
 */
 
148
 
 
149
void Curl_failf(struct SessionHandle *data, const char *fmt, ...)
 
150
{
 
151
  va_list ap;
 
152
  va_start(ap, fmt);
 
153
  if(data->set.errorbuffer && !data->state.errorbuf) {
 
154
    vsnprintf(data->set.errorbuffer, CURL_ERROR_SIZE, fmt, ap);
 
155
    data->state.errorbuf = TRUE; /* wrote error string */
 
156
  }
 
157
  va_end(ap);
 
158
}
 
159
 
 
160
/* Curl_sendf() sends formated data to the server */
 
161
CURLcode Curl_sendf(int sockfd, struct connectdata *conn,
 
162
                    const char *fmt, ...)
 
163
{
 
164
  struct SessionHandle *data = conn->data;
 
165
  ssize_t bytes_written;
 
166
  CURLcode result;
 
167
  char *s;
 
168
  va_list ap;
 
169
  va_start(ap, fmt);
 
170
  s = vaprintf(fmt, ap); /* returns an allocated string */
 
171
  va_end(ap);
 
172
  if(!s)
 
173
    return 0; /* failure */
 
174
  if(data->set.verbose)
 
175
    fprintf(data->set.err, "> %s", s);
 
176
 
 
177
  /* Write the buffer to the socket */
 
178
  result = Curl_write(conn, sockfd, s, strlen(s), &bytes_written);
 
179
 
 
180
  free(s); /* free the output string */
 
181
 
 
182
  return result;
 
183
}
 
184
 
 
185
/*
 
186
 * Curl_write() is an internal write function that sends plain (binary) data
 
187
 * to the server. Works with plain sockets, SSL or kerberos.
 
188
 *
 
189
 */
 
190
CURLcode Curl_write(struct connectdata *conn, int sockfd,
 
191
                    void *mem, size_t len,
 
192
                    ssize_t *written)
 
193
{
 
194
  ssize_t bytes_written;
 
195
 
 
196
#ifdef USE_SSLEAY
 
197
  /* SSL_write() is said to return 'int' while write() and send() returns
 
198
     'size_t' */
 
199
  if (conn->ssl.use) {
 
200
    int err;
 
201
    int rc = SSL_write(conn->ssl.handle, mem, len);
 
202
 
 
203
    if(rc < 0) {
 
204
      err = SSL_get_error(conn->ssl.handle, rc);
 
205
    
 
206
      switch(err) {
 
207
      case SSL_ERROR_WANT_READ:
 
208
      case SSL_ERROR_WANT_WRITE:
 
209
        /* this is basicly the EWOULDBLOCK equivalent */
 
210
        *written = 0;
 
211
        return CURLE_OK;
 
212
      }
 
213
      /* a true error */
 
214
      failf(conn->data, "SSL_write() return error %d\n", err);
 
215
      return CURLE_WRITE_ERROR;
 
216
    }
 
217
    bytes_written = rc;
 
218
  }
 
219
  else {
 
220
#endif
 
221
#ifdef KRB4
 
222
    if(conn->sec_complete) {
 
223
      bytes_written = Curl_sec_write(conn, sockfd, mem, len);
 
224
    }
 
225
    else
 
226
#endif /* KRB4 */
 
227
    {
 
228
      bytes_written = swrite(sockfd, mem, len);
 
229
    }
 
230
    if(-1 == bytes_written) {
 
231
#ifdef WIN32
 
232
      if(WSAEWOULDBLOCK == GetLastError())
 
233
#else
 
234
      if(EWOULDBLOCK == errno)
 
235
#endif
 
236
      {
 
237
        /* this is just a case of EWOULDBLOCK */
 
238
        *written=0;
 
239
        return CURLE_OK;
 
240
      }
 
241
    }
 
242
#ifdef USE_SSLEAY
 
243
  }
 
244
#endif
 
245
 
 
246
  *written = bytes_written;
 
247
  return (-1 != bytes_written)?CURLE_OK:CURLE_WRITE_ERROR;
 
248
}
 
249
 
 
250
/* client_write() sends data to the write callback(s)
 
251
 
 
252
   The bit pattern defines to what "streams" to write to. Body and/or header.
 
253
   The defines are in sendf.h of course.
 
254
 */
 
255
CURLcode Curl_client_write(struct SessionHandle *data,
 
256
                           int type,
 
257
                           char *ptr,
 
258
                           size_t len)
 
259
{
 
260
  size_t wrote;
 
261
 
 
262
  if(0 == len)
 
263
    len = strlen(ptr);
 
264
 
 
265
  if(type & CLIENTWRITE_BODY) {
 
266
    wrote = data->set.fwrite(ptr, 1, len, data->set.out);
 
267
    if(wrote != len) {
 
268
      failf (data, "Failed writing body");
 
269
      return CURLE_WRITE_ERROR;
 
270
    }
 
271
  }
 
272
  if((type & CLIENTWRITE_HEADER) &&
 
273
     (data->set.fwrite_header || data->set.writeheader) ) {
 
274
    /*
 
275
     * Write headers to the same callback or to the especially setup
 
276
     * header callback function (added after version 7.7.1).
 
277
     */
 
278
    curl_write_callback writeit=
 
279
      data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite;
 
280
 
 
281
    wrote = writeit(ptr, 1, len, data->set.writeheader);
 
282
    if(wrote != len) {
 
283
      failf (data, "Failed writing header");
 
284
      return CURLE_WRITE_ERROR;
 
285
    }
 
286
  }
 
287
  
 
288
  return CURLE_OK;
 
289
}
 
290
 
 
291
/*
 
292
 * Internal read-from-socket function. This is meant to deal with plain
 
293
 * sockets, SSL sockets and kerberos sockets.
 
294
 *
 
295
 * If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
 
296
 * a regular CURLcode value.
 
297
 */
 
298
int Curl_read(struct connectdata *conn,
 
299
              int sockfd,
 
300
              char *buf,
 
301
              size_t buffersize,
 
302
              ssize_t *n)
 
303
{
 
304
  ssize_t nread;
 
305
 
 
306
#ifdef USE_SSLEAY
 
307
  if (conn->ssl.use) {
 
308
    bool loop=TRUE;
 
309
    int err;
 
310
    do {
 
311
      nread = SSL_read(conn->ssl.handle, buf, buffersize);
 
312
 
 
313
      if(nread >= 0)
 
314
        /* successful read */
 
315
        break;
 
316
 
 
317
      err = SSL_get_error(conn->ssl.handle, nread);
 
318
 
 
319
      switch(err) {
 
320
      case SSL_ERROR_NONE: /* this is not an error */
 
321
      case SSL_ERROR_ZERO_RETURN: /* no more data */
 
322
        loop=0; /* get out of loop */
 
323
        break;
 
324
      case SSL_ERROR_WANT_READ:
 
325
      case SSL_ERROR_WANT_WRITE:
 
326
        /* if there's data pending, then we re-invoke SSL_read() */
 
327
        break;
 
328
      }
 
329
    } while(loop);
 
330
    if(loop && SSL_pending(conn->ssl.handle))
 
331
      return -1; /* basicly EWOULDBLOCK */
 
332
  }
 
333
  else {
 
334
#endif
 
335
#ifdef KRB4
 
336
    if(conn->sec_complete)
 
337
      nread = Curl_sec_read(conn, sockfd, buf, buffersize);
 
338
    else
 
339
#endif
 
340
      nread = sread (sockfd, buf, buffersize);
 
341
 
 
342
    if(-1 == nread) {
 
343
#ifdef WIN32
 
344
      if(WSAEWOULDBLOCK == GetLastError())
 
345
#else
 
346
      if(EWOULDBLOCK == errno)
 
347
#endif
 
348
        return -1;
 
349
    }
 
350
 
 
351
#ifdef USE_SSLEAY
 
352
  }
 
353
#endif /* USE_SSLEAY */
 
354
  *n = nread;
 
355
  return CURLE_OK;
 
356
}
 
357
 
 
358
 
 
359
/*
 
360
 * local variables:
 
361
 * eval: (load-file "../curl-mode.el")
 
362
 * end:
 
363
 * vim600: fdm=marker
 
364
 * vim: et sw=2 ts=2 sts=2 tw=78
 
365
 */