~ubuntu-branches/ubuntu/edgy/curl/edgy

« back to all changes in this revision

Viewing changes to lib/sendf.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-06-29 15:04:24 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060629150424-pn00qumt9sml8p4m
Tags: 7.15.4-1ubuntu1
Synchronize to Debian. Only change left: Removal of stunnel and
libdb4.2-dev build dependencies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: sendf.c,v 1.98 2005/04/07 15:27:14 bagder Exp $
 
21
 * $Id: sendf.c,v 1.102 2006-05-05 10:24:27 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
43
43
#include <curl/curl.h>
44
44
#include "urldata.h"
45
45
#include "sendf.h"
46
 
#include "connect.h" /* for the Curl_ourerrno() proto */
 
46
#include "connect.h" /* for the Curl_sockerrno() proto */
47
47
#include "sslgen.h"
48
48
 
49
49
#define _MPRINTF_REPLACE /* use the internal *printf() functions */
59
59
#include <string.h>
60
60
#include "memory.h"
61
61
#include "strerror.h"
 
62
#include "easyif.h" /* for the Curl_convert_from_network prototype */
62
63
/* The last #include file should be: */
63
64
#include "memdebug.h"
64
65
 
138
139
  } while (next);
139
140
}
140
141
 
 
142
#ifdef CURL_DO_LINEEND_CONV
 
143
/*
 
144
 * convert_lineends() changes CRLF (\r\n) end-of-line markers to a single LF
 
145
 * (\n), with special processing for CRLF sequences that are split between two
 
146
 * blocks of data.  Remaining, bare CRs are changed to LFs.  The possibly new
 
147
 * size of the data is returned.
 
148
 */
 
149
static size_t convert_lineends(struct SessionHandle *data,
 
150
                               char *startPtr, size_t size)
 
151
{
 
152
  char *inPtr, *outPtr;
 
153
 
 
154
  /* sanity check */
 
155
  if ((startPtr == NULL) || (size < 1)) {
 
156
    return(size);
 
157
  }
 
158
 
 
159
  if (data->state.prev_block_had_trailing_cr == TRUE) {
 
160
    /* The previous block of incoming data
 
161
       had a trailing CR, which was turned into a LF. */
 
162
    if (*startPtr == '\n') {
 
163
      /* This block of incoming data starts with the
 
164
         previous block's LF so get rid of it */
 
165
      memcpy(startPtr, startPtr+1, size-1);
 
166
      size--;
 
167
      /* and it wasn't a bare CR but a CRLF conversion instead */
 
168
      data->state.crlf_conversions++;
 
169
    }
 
170
    data->state.prev_block_had_trailing_cr = FALSE; /* reset the flag */
 
171
  }
 
172
 
 
173
  /* find 1st CR, if any */
 
174
  inPtr = outPtr = memchr(startPtr, '\r', size);
 
175
  if (inPtr) {
 
176
    /* at least one CR, now look for CRLF */
 
177
    while (inPtr < (startPtr+size-1)) {
 
178
      /* note that it's size-1, so we'll never look past the last byte */
 
179
      if (memcmp(inPtr, "\r\n", 2) == 0) {
 
180
        /* CRLF found, bump past the CR and copy the NL */
 
181
        inPtr++;
 
182
        *outPtr = *inPtr;
 
183
        /* keep track of how many CRLFs we converted */
 
184
        data->state.crlf_conversions++;
 
185
      }
 
186
      else {
 
187
        if (*inPtr == '\r') {
 
188
          /* lone CR, move LF instead */
 
189
          *outPtr = '\n';
 
190
        }
 
191
        else {
 
192
          /* not a CRLF nor a CR, just copy whatever it is */
 
193
          *outPtr = *inPtr;
 
194
        }
 
195
      }
 
196
      outPtr++;
 
197
      inPtr++;
 
198
    } /* end of while loop */
 
199
 
 
200
    if (inPtr < startPtr+size) {
 
201
      /* handle last byte */
 
202
      if (*inPtr == '\r') {
 
203
        /* deal with a CR at the end of the buffer */
 
204
        *outPtr = '\n'; /* copy a NL instead */
 
205
        /* note that a CRLF might be split across two blocks */
 
206
        data->state.prev_block_had_trailing_cr = TRUE;
 
207
      }
 
208
      else {
 
209
        /* copy last byte */
 
210
        *outPtr = *inPtr;
 
211
      }
 
212
      outPtr++;
 
213
      inPtr++;
 
214
    }
 
215
    if (outPtr < startPtr+size) {
 
216
      /* tidy up by null terminating the now shorter data */
 
217
      *outPtr = '\0';
 
218
    }
 
219
    return(outPtr - startPtr);
 
220
  }
 
221
  return(size);
 
222
}
 
223
#endif /* CURL_DO_LINEEND_CONV */
 
224
 
141
225
/* Curl_infof() is for info message along the way */
142
226
 
143
227
void Curl_infof(struct SessionHandle *data, const char *fmt, ...)
251
335
      bytes_written = (ssize_t)swrite(sockfd, mem, len);
252
336
 
253
337
    if(-1 == bytes_written) {
254
 
      int err = Curl_ourerrno();
 
338
      int err = Curl_sockerrno();
255
339
 
256
340
      if(
257
341
#ifdef WSAEWOULDBLOCK
294
378
    len = strlen(ptr);
295
379
 
296
380
  if(type & CLIENTWRITE_BODY) {
297
 
    wrote = data->set.fwrite(ptr, 1, len, data->set.out);
 
381
    if(data->ftp_in_ascii_mode) {
 
382
#ifdef CURL_DOES_CONVERSIONS
 
383
      /* convert from the network encoding */
 
384
      size_t rc;
 
385
      rc = Curl_convert_from_network(data, ptr, len);
 
386
      /* Curl_convert_from_network calls failf if unsuccessful */
 
387
      if(rc != CURLE_OK)
 
388
        return rc;
 
389
#endif /* CURL_DOES_CONVERSIONS */
 
390
 
 
391
#ifdef CURL_DO_LINEEND_CONV
 
392
      /* convert end-of-line markers */
 
393
      len = convert_lineends(data, ptr, len);
 
394
#endif /* CURL_DO_LINEEND_CONV */
 
395
    }
 
396
    /* If the previous block of data ended with CR and this block of data is
 
397
       just a NL, then the length might be zero */
 
398
    if (len) {
 
399
      wrote = data->set.fwrite(ptr, 1, len, data->set.out);
 
400
    }
 
401
    else {
 
402
      wrote = len;
 
403
    }
 
404
 
298
405
    if(wrote != len) {
299
406
      failf (data, "Failed writing body");
300
407
      return CURLE_WRITE_ERROR;
301
408
    }
302
409
  }
 
410
 
303
411
  if((type & CLIENTWRITE_HEADER) &&
304
412
     (data->set.fwrite_header || data->set.writeheader) ) {
305
413
    /*
309
417
    curl_write_callback writeit=
310
418
      data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite;
311
419
 
 
420
    /* Note: The header is in the host encoding
 
421
       regardless of the ftp transfer mode (ASCII/Image) */
 
422
 
312
423
    wrote = writeit(ptr, 1, len, data->set.writeheader);
313
424
    if(wrote != len) {
314
425
      failf (data, "Failed writing header");
355
466
      nread = sread(sockfd, buf, buffersize);
356
467
 
357
468
    if(-1 == nread) {
358
 
      int err = Curl_ourerrno();
 
469
      int err = Curl_sockerrno();
359
470
#ifdef WIN32
360
471
      if(WSAEWOULDBLOCK == err)
361
472
#else
375
486
  static const char * const s_infotype[CURLINFO_END] = {
376
487
    "* ", "< ", "> ", "{ ", "} ", "{ ", "} " };
377
488
 
 
489
#ifdef CURL_DOES_CONVERSIONS
 
490
  char buf[BUFSIZE+1];
 
491
 
 
492
  switch(type) {
 
493
  case CURLINFO_HEADER_OUT:
 
494
    /* assume output headers are ASCII */
 
495
    /* copy the data into my buffer so the original is unchanged */
 
496
    if (size > BUFSIZE) {
 
497
      size = BUFSIZE; /* truncate if necessary */
 
498
      buf[BUFSIZE] = '\0';
 
499
    }
 
500
    memcpy(buf, ptr, size);
 
501
    Curl_convert_from_network(data, buf, size);
 
502
    /* Curl_convert_from_network calls failf if unsuccessful */
 
503
    /* we might as well continue even if it fails...   */
 
504
    ptr = buf; /* switch pointer to use my buffer instead */
 
505
    break;
 
506
  default:
 
507
    /* leave everything else as-is */
 
508
    break;
 
509
  }
 
510
#endif /* CURL_DOES_CONVERSIONS */
 
511
 
378
512
  if(data->set.fdebug)
379
513
    return (*data->set.fdebug)(data, type, ptr, size,
380
514
                               data->set.debugdata);