Revision 1.1 |
Revision 1.3 |
Line 429 |
Line 429 |
return rv; }  
|
return rv; }  
|
|
/****
* Reads a complete line from the socket into the provided buffer. A
* line is defined as a sequence of characters ending with a \n. The \n
* and, if present, the \r before it are stripped before being copied to
* the buffer.
*
* @param in The socket to read from
* @param buf The buffer to read into
* @param nbyte Size of buffer; TODO: either remove or check this
* @param timeout Give up after waiting this long
*
* @return The length of the line copied to the buffer. If we timeout
* before reading an entire line then -1 * (chars read + 1) is returned
* and errno is set to EINTR.
****/
int cow_read_line(BUFF* in, char* buf, int nbyte, pth_event_t timeout) {
int i, nrd;
char* eol;
/* If there's a socket error, just return */
if (in->flags & B_RDERR)
return -1;
/* Is there a complete line in the buffer? */
nrd = in->incnt;
if (nrd > 0) {
eol = strchr(in->inptr, '\n');
if (eol != NULL) {
int len = (eol - (char*)in->inptr);
memcpy(buf, in->inptr, len);
in->incnt = nrd - (len+1);
in->inptr += (len+1);
/* Null terminate the user buffer */
if (len > 0 && buf[len - 1] == '\r') {
buf[len - 1] = '\0';
return len - 1;
} else {
buf[len] = '\0';
return len;
}
}
}
/*
* Need to read some more from the socket. Start by copying what we've
* already got.
*/
if (nrd > 0) {
memcpy(buf, in->inptr, nrd);
buf = nrd + (char *) buf;
}
in->incnt = 0;
in->inptr = in->inbase;
in->inptr[0] = '\0';
if (in->flags & B_EOF)
return nrd;
/*
* Read as much as we can directly into the user buffer. If we read more
* than one line in, copy the excess back into the hold buffer.
*/
i = read_with_errors(in, buf, nbyte, timeout);
if (i <= 0 || errno == EINTR)
return -1;
eol = strchr(buf, '\n');
if (eol != NULL) {
int len;
/* Copy excess into hold buffer */
if (eol != buf + i - 1) {
in->incnt = i - (eol - buf) - 1;
memcpy(in->inbase, eol+1, in->incnt);
in->inptr = in->inbase;
} else {
in->incnt = 0;
in->inptr = in->inbase;
}
/* Add null-terminator to user buffer */
len = eol - buf;
if (len > 0 && buf[len-1] == '\r') {
buf[len-1] = '\0';
return len - 1;
} else {
buf[len] = '\0';
return len;
}
}
/*
* We read some stuff, but we still haven't seen that newline...
* Call ourselves recursively to get the rest.
*/
return cow_read_line(in, buf+i, nbyte-i, timeout);
}
|
/* * Read up to nbyte bytes into buf.
|
/* * Read up to nbyte bytes into buf.
|
Line 892 |
Line 990 |
int rv; pth_event_t ev = pth_timeout(COW_WTO_S, COW_WTO_U); rv = pth_write_ev(out, buf, nbyte, ev);
|
int rv; pth_event_t ev = pth_timeout(COW_WTO_S, COW_WTO_U); rv = pth_write_ev(out, buf, nbyte, ev);
|
|
if (rv < nbyte && error == EINTR) {
printf("DEBUG: Writing timed out!\n");
} else if (rv < nbyte) {
printf("DEBUG: Error writing!\n");
}
|
pth_event_free(ev, PTH_FREE_THIS); return rv; #else /* if not USE_TCPCORK */
|
pth_event_free(ev, PTH_FREE_THIS); return rv; #else /* if not USE_TCPCORK */
|
Line 1095 |
Line 1200 |
return rc1 || rc2 || rc3; }
|
return rc1 || rc2 || rc3; }
|
/*
* returns the number of bytes written or -1 on error
*/
int ap_bputs(const char *x, BUFF *fb)
{
int i, j = strlen(x);
i = cow_write(fb, x, j);
if (i != j)
return -1;
else
return j;
}
/*
* returns the number of bytes written or -1 on error
*/
int ap_bvputs(BUFF *fb,...)
{
int i, j, k;
va_list v;
const char *x;
va_start(v, fb);
for (k = 0;;) {
x = va_arg(v, const char *);
if (x == NULL)
break;
j = strlen(x);
i = cow_write(fb, x, j);
if (i != j) {
va_end(v);
return -1;
}
k += i;
}
va_end(v);
return k;
}
|
|
void ap_bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), void *data) {
|
void ap_bonerror(BUFF *fb, void (*error) (BUFF *, int, void *), void *data) {
|
Line 1144 |
Line 1208 |
}
|
}
|