Beispiel #1
0
/* -------------------------------------------------- */
static int wrap_line(int fd, char *buff, int len)
{
static int done =0, state = 0;
int rc=0, idx=0;
static char temp[512];

#if (WANT_DEBUG &2)
fprintf(stderr, " [Wrap_line(%d,%p,%d)]\n", fd, buff, len);
#endif

if (!buff) {
	len = 0;
	if (state) { memcpy(temp+done, "\r\n", 2); done += 2; }
	state = 0;
	}
else for (idx = 0; idx < len; idx++) {
	switch(buff[idx]) {
	case '.' :
		if (!state) temp[done++] = '.' ;
		state = 1; goto dodo;
	case '\r' :
		break;
	case '\n' :
		temp[done++] = '\r' ;
		state = 0; goto dodo;
	default:
		state = 1;
	dodo:
		temp[done++] = buff[idx];
		}
	if (done+2 < (int) sizeof temp) continue;
	rc = wrap_write(fd, temp, done);
#if (WANT_DEBUG &2)
	fprintf(stderr, " [Wrap_line(%d,%d) Loop :=%d,%d]\n", fd,len,done,rc);
#endif
	if (rc <=0) goto kut;
	if (rc < done) memmove(temp, temp + rc, done - rc);
	done -= rc;
	}

if (done)	{
	rc = wrap_write(fd, temp, done);
#if (WANT_DEBUG &2)
	fprintf(stderr, " [Wrap_line(%d,%d) Flush :=%d,%d]\n"
		, fd, len, done,rc);
#endif
	if (rc <=0) goto kut;
	if (rc < done) memmove(temp, temp + rc, done - rc);
	done -= rc;
	}
kut:
#if (WANT_DEBUG &2)
fprintf(stderr, " [Wrap_line(i=%d d=%d r=%d)]\n", idx, done, rc);
/* sleep(1); */

#endif

return rc <= 0 ? -1 :idx;
}
int wrap_vprintf (void *data, const char *fmt, va_list ap)
{
  char buf[4096];
  int len;

  len = ap_vsnprintf (buf, sizeof(buf), fmt, ap);
  return wrap_write (data, buf, len);
}
void rand_fasta(const amino *s, size_t outlen) {
   printf("void rand_fasta(const amino *s, size_t outlen) {\n");
   int fd = fileno(stdout);
   char buf[WIDTH+1];

   int i, j, k;
   float sum = 0;
   for (i = j = k = 0; s[i].p && k < RMAX; i++) {
      printf(" for (i = j = k = 0; s[i].p && k < RMAX; i++) {\n");
      if (s[i].p) {
         printf(" if (s[i].p) {\n");
         sum += s[i].p;
         k = RMAX * sum + 1;
      } else {
         printf(" } else {\n");
         k = RMAX;
      }
      if (k > RMAX) {
          printf(" if (k > RMAX) {\n");
          k = RMAX;
      }
      memset(lookup + j, s[i].c, k - j);
      j = k;
   }

   i = 0;
   buf[WIDTH] = '\n';
   while (outlen--) {
      printf(" while (outlen--) {\n");
      buf[i++] = lookup[rnd()];
      if (i == WIDTH) {
         printf(" if (i == WIDTH) {\n");
         wrap_write(fd, buf, WIDTH + 1);
         i = 0;
      }
   }
   if (i) {
      printf(" if (i) {\n");
      buf[i] = '\n';
      wrap_write(fd, buf, i + 1);
   }
}
Beispiel #4
0
/* -------------------------------------------------- */
static int add_data(int fd, char *buff, int len)
{
int rc, idx;

if (!buff) {
	char pipo[] = ".\r\n\0";
	wrap_line(fd, NULL, 0); /* flush */
	return wrap_write(fd, pipo, 3); /* writable strings ... avoid . -> .. */
	}
if (len <= 0) len = strlen(buff);

for (idx=0; idx < len; idx += rc) {
	rc = wrap_line(fd, buff+idx, len - idx);
	if (rc < 0) {close(fd); return -1; }
	}

return idx;
}
void str_repeat(const char *s, int outlen) {
   printf("void str_repeat(const char *s, int outlen) {\n");
   int len = strlen(s) * (1 + WIDTH);
   outlen += outlen / WIDTH;

   const char *ss = s;
   char *buf = malloc(len);
   int pos = 0;

   while (pos < len) {
      printf(" while (pos < len) {\n");
      if (!*ss) {
          printf(" if (!*ss) {\n");
          ss = s;
      }
      buf[pos++] = *ss++;
      if (pos >= len) {
          printf(" if (pos >= len) {\n");
          break;
      }
      if (pos % (WIDTH + 1) == WIDTH) {
         printf(" if (pos %% (WIDTH + 1) == WIDTH) {\n");
         buf[pos++] = '\n';
      }
   }

   int fd = fileno(stdout);
   int l = 0;
   while (outlen > 0) {
      printf(" while (outlen > 0) {\n");
      l = outlen > len ? len : outlen;
      wrap_write(fd, buf, l);
      outlen -= len;
   }
   if (buf[l-1] != '\n') {
       printf(" if (buf[l-1] != '\n') {\n");
       str_write("\n");
   }

   free(buf);
}
inline void str_write(char *s) {
   printf("inline void str_write(char *s) {\n");
   wrap_write(fileno(stdout), s, strlen(s));
}
Beispiel #7
0
void write_request (struct REQUEST *req)
{
    int rc;

    for (;;) {
        switch (req->state) {
        case STATE_WRITE_HEADER:
#ifdef TCP_CORK

            if (0 == req->tcp_cork && !req->head_only) {
                req->tcp_cork = 1;

                if (debug) {
                    fprintf (stderr, "%03d: tcp_cork=%d\n", req->fd, req->tcp_cork);
                }

                setsockopt (req->fd, SOL_TCP, TCP_CORK, &req->tcp_cork, sizeof (int) );
            }

#endif
            rc = wrap_write (req, req->hres + req->written,
                             req->lres - req->written);

            switch (rc) {
            case -1:

                if (errno == EAGAIN) {
                    return;
                }

                if (errno == EINTR) {
                    continue;
                }

                xperror (LOG_INFO, "write", req->peerhost);
            /* fall through */
            case 0:
                req->state = STATE_CLOSE;
                return;
            default:
                req->written += rc;
                req->bc += rc;

                if (req->written != req->lres) {
                    return;
                }
            }

            req->written = 0;

            if (req->head_only) {
                req->state = STATE_FINISHED;
                return;
            } else if (req->cgipid) {
                req->state = (req->cgipos != req->cgilen) ?
                             STATE_CGI_BODY_OUT : STATE_CGI_BODY_IN;
            } else if (req->body) {
                req->state = STATE_WRITE_BODY;
            } else if (req->ranges == 1) {
                req->state = STATE_WRITE_RANGES;
                req->rh = -1;
                req->rb = 0;
                req->written = req->r_start[0];
            } else if (req->ranges > 1) {
                req->state = STATE_WRITE_RANGES;
                req->rh = 0;
                req->rb = -1;
            } else {
                req->state = STATE_WRITE_FILE;
            }

            break;
        case STATE_WRITE_BODY:
            rc = wrap_write (req, req->body + req->written,
                             req->lbody - req->written);

            switch (rc) {
            case -1:

                if (errno == EAGAIN) {
                    return;
                }

                if (errno == EINTR) {
                    continue;
                }

                xperror (LOG_INFO, "write", req->peerhost);
            /* fall through */
            case 0:
                req->state = STATE_CLOSE;
                return;
            default:
                req->written += rc;
                req->bc += rc;

                if (req->written != req->lbody) {
                    return;
                }
            }

            req->state = STATE_FINISHED;
            return;
        case STATE_WRITE_FILE:
            rc = wrap_xsendfile (req, req->written,
                                 req->bst.st_size - req->written);

            switch (rc) {
            case -1:

                if (errno == EAGAIN) {
                    return;
                }

                if (errno == EINTR) {
                    continue;
                }

                xperror (LOG_INFO, "sendfile", req->peerhost);
            /* fall through */
            case 0:
                req->state = STATE_CLOSE;
                return;
            default:

                if (debug > 1)
                    fprintf (stderr, "%03d: %" PRId64 "/%" PRId64 " (%d%%)\r", req->fd,
                             (int64_t) req->written, (int64_t) req->bst.st_size,
                             (int) (req->written * 100 / req->bst.st_size) );

                req->written += rc;
                req->bc += rc;

                if (req->written != req->bst.st_size) {
                    return;
                }
            }

            req->state = STATE_FINISHED;
            return;
        case STATE_WRITE_RANGES:

            if (-1 != req->rh) {
                /* write header */
                rc = wrap_write (req,
                                 req->r_head + req->rh * BR_HEADER + req->written,
                                 req->r_hlen[req->rh] - req->written);

                switch (rc) {
                case -1:

                    if (errno == EAGAIN) {
                        return;
                    }

                    if (errno == EINTR) {
                        continue;
                    }

                    xperror (LOG_INFO, "write", req->peerhost);
                /* fall through */
                case 0:
                    req->state = STATE_CLOSE;
                    return;
                default:
                    req->written += rc;
                    req->bc += rc;

                    if (req->written != req->r_hlen[req->rh]) {
                        return;
                    }
                }

                if (req->rh == req->ranges) {
                    /* done -- no more ranges */
                    req->state = STATE_FINISHED;
                    return;
                }

                /* prepare for body writeout */
                req->rb      = req->rh;
                req->rh      = -1;
                req->written = req->r_start[req->rb];
            }

            if (-1 != req->rb) {
                /* write body */
                rc = wrap_xsendfile (req, req->written,
                                     req->r_end[req->rb] - req->written);

                switch (rc) {
                case -1:

                    if (errno == EAGAIN) {
                        return;
                    }

                    if (errno == EINTR) {
                        continue;
                    }

                    xperror (LOG_INFO, "sendfile", req->peerhost);
                /* fall through */
                case 0:
                    req->state = STATE_CLOSE;
                    return;
                default:
                    req->written += rc;
                    req->bc += rc;

                    if (req->written != req->r_end[req->rb]) {
                        return;
                    }
                }

                /* prepare for next subheader writeout */
                req->rh      = req->rb + 1;
                req->rb      = -1;
                req->written = 0;

                if (req->ranges == 1) {
                    /* single range only */
                    req->state = STATE_FINISHED;
                    return;
                }
            }

            break;
        case STATE_CGI_BODY_IN:
            rc = read (req->cgipipe, req->cgibuf, MAX_HEADER);

            switch (rc) {
            case -1:

                if (errno == EAGAIN) {
                    return;
                }

                if (errno == EINTR) {
                    continue;
                }

                xperror (LOG_INFO, "cgi read", req->peerhost);
            /* fall through */
            case 0:
                req->state = STATE_FINISHED;
                return;
            default:

                if (debug) {
                    fprintf (stderr, "%03d: cgi: in %d\n", req->fd, rc);
                }

                req->cgipos = 0;
                req->cgilen = rc;
                break;
            }

            req->state = STATE_CGI_BODY_OUT;
            break;
        case STATE_CGI_BODY_OUT:
            rc = wrap_write (req, req->cgibuf + req->cgipos,
                             req->cgilen - req->cgipos);

            switch (rc) {
            case -1:

                if (errno == EAGAIN) {
                    return;
                }

                if (errno == EINTR) {
                    continue;
                }

                xperror (LOG_INFO, "write", req->peerhost);
            /* fall through */
            case 0:
                req->state = STATE_CLOSE;
                return;
            default:

                if (debug) {
                    fprintf (stderr, "%03d: cgi: out %d\n", req->fd, rc);
                }

                req->cgipos += rc;
                req->bc += rc;

                if (req->cgipos != req->cgilen) {
                    return;
                }
            }

            req->state = STATE_CGI_BODY_IN;
            break;
        } /* switch(state) */
    } /* for (;;) */
}