Ejemplo n.º 1
0
/*
 *----------------------------------------------------------------------
 *
 * ReadParams --
 *
 *      Reads FastCGI name-value pairs from stream until EOF.  Converts
 *      each pair to name=value format and adds it to Params structure.
 *
 *----------------------------------------------------------------------
 */
static int ReadParams(Params * paramsPtr, FCGX_Stream * stream)
{
	int nameLen, valueLen;
	unsigned char lenBuff[3];
	char *nameValue;

	while ((nameLen = FCGX_GetChar(stream)) != EOF) {
		/*
		 * Read name length (one or four bytes) and value length
		 * (one or four bytes) from stream.
		 */
		if ((nameLen & 0x80) != 0) {
			if (FCGX_GetStr((char *)&lenBuff[0], 3, stream) != 3) {
				SetError(stream, FCGX_PARAMS_ERROR);
				return -1;
			}
			nameLen = ((nameLen & 0x7f) << 24) + (lenBuff[0] << 16)
				+ (lenBuff[1] << 8) + lenBuff[2];
		}
		if ((valueLen = FCGX_GetChar(stream)) == EOF) {
			SetError(stream, FCGX_PARAMS_ERROR);
			return -1;
		}
		if ((valueLen & 0x80) != 0) {
			if (FCGX_GetStr((char *)&lenBuff[0], 3, stream) != 3) {
				SetError(stream, FCGX_PARAMS_ERROR);
				return -1;
			}
			valueLen =
				((valueLen & 0x7f) << 24) + (lenBuff[0] << 16)
				+ (lenBuff[1] << 8) + lenBuff[2];
		}
		/*
		 * nameLen and valueLen are now valid; read the name and value
		 * from stream and construct a standard environment entry.
		 */
		nameValue = (char *)Malloc(nameLen + valueLen + 2);
		if (FCGX_GetStr(nameValue, nameLen, stream) != nameLen) {
			SetError(stream, FCGX_PARAMS_ERROR);
			free(nameValue);
			return -1;
		}
		*(nameValue + nameLen) = '=';
		if (FCGX_GetStr(nameValue + nameLen + 1, valueLen, stream)
			!= valueLen) {
			SetError(stream, FCGX_PARAMS_ERROR);
			free(nameValue);
			return -1;
		}
		*(nameValue + nameLen + valueLen + 1) = '\0';
		PutParam(paramsPtr, nameValue);
	}
	return 0;
}
Ejemplo n.º 2
0
int request_post(FCGX_Stream *in, FCGX_Stream *out, FCGX_ParamArray *envp)
{
	// have not test
	char *contentSplit;  
	char *buffer = NULL;
  
    char *content_type = FCGX_GetParam("CONTENT_TYPE", *envp); 
    contentSplit = strchr(content_type,'=')+1; 
    char *content_length = FCGX_GetParam("CONTENT_LENGTH", *envp); 
	int len = 0;

    if(content_length != NULL)  
    {  
        len = strtol(content_length, NULL, 10);  
        if(len == 0)  
        {  
			FCGX_FPrintF(out, "No data posted\n");  
        }else  
        {  
            if(len > MAX_LEN)                     
                len = MAX_LEN;  
            buffer = (char*)malloc(len);  
            if(buffer)  
            {  
                for(int i=0;i<len;i++)  
                {  
                    buffer[i] = FCGX_GetChar(in);  
                }  
                post_data_handle(buffer,len,contentSplit,out);  
                free(buffer);  
            }  
        }  
    }
	return 0;
}
Ejemplo n.º 3
0
int FCGI_fgetc(FCGI_FILE *fp)
{
    if(fp->stdio_stream)
        return fgetc(fp->stdio_stream);
    else if(fp->fcgx_stream)
        return FCGX_GetChar(fp->fcgx_stream);
    return EOF;
}
Ejemplo n.º 4
0
static VALUE fcgi_stream_getc(VALUE self)
{
  FCGX_Stream *stream;
  int c;

  Data_Get_Struct(self, FCGX_Stream, stream);
  if ((c = FCGX_GetChar(stream)) == EOF) {
    CHECK_STREAM_ERROR(stream);
    return Qnil;
  }
  else {
    return INT2NUM(c);
  }
}
Ejemplo n.º 5
0
int main ()
{
    FCGX_Stream *in, *out, *err;
    FCGX_ParamArray envp;
    int count = 0;

    while (FCGX_Accept(&in, &out, &err, &envp) >= 0) {
        char *contentLength = FCGX_GetParam("CONTENT_LENGTH", envp);
        int len = 0;

        FCGX_FPrintF(out,
           "Content-type: text/html\r\n"
           "\r\n"
           "<title>FastCGI echo (fcgiapp version)</title>"
           "<h1>FastCGI echo (fcgiapp version)</h1>\n"
           "Request number %d,  Process ID: %d<p>\n", ++count, getpid());

        if (contentLength != NULL)
            len = strtol(contentLength, NULL, 10);

        if (len <= 0) {
            FCGX_FPrintF(out, "No data from standard input.<p>\n");
        }
        else {
            int i, ch;

            FCGX_FPrintF(out, "Standard input:<br>\n<pre>\n");
            for (i = 0; i < len; i++) {
                if ((ch = FCGX_GetChar(in)) < 0) {
                    FCGX_FPrintF(out,
                        "Error: Not enough bytes received on standard input<p>\n");
                    break;
                }
                FCGX_PutChar(ch, out);
            }
            FCGX_FPrintF(out, "\n</pre><p>\n");
        }

        PrintEnv(out, "Request environment", envp);
        PrintEnv(out, "Initial environment", environ);
    } /* while */

    return 0;
}
Ejemplo n.º 6
0
/*
 *----------------------------------------------------------------------
 *
 * FCGX_GetLine --
 *
 *      Reads up to n-1 consecutive bytes from the input stream
 *      into the character array str.  Stops before n-1 bytes
 *      have been read if '\n' or EOF is read.  The terminating '\n'
 *      is copied to str.  After copying the last byte into str,
 *      stores a '\0' terminator.
 *
 * Results:
 *	NULL if EOF is the first thing read from the input stream,
 *      str otherwise.
 *
 *----------------------------------------------------------------------
 */
char *FCGX_GetLine(char *str, int n, FCGX_Stream * stream)
{
	int c;
	char *p = str;

	n--;
	while (n > 0) {
		c = FCGX_GetChar(stream);
		if (c == EOF) {
			if (p == str)
				return NULL;
			else
				break;
		}
		*p++ = (char)c;
		n--;
		if (c == '\n')
			break;
	}
	*p = '\0';
	return str;
}
Ejemplo n.º 7
0
/*
 * read CONTENT_LENGTH input and return as string
 */
int
clip_FCGI_READ(ClipMachine *mp)
{
	char *clen;
	int len;

	if (!inited)
		return EG_ARG;

	clen =  FCGX_GetParam("CONTENT_LENGTH", envp);
	/*clen =  getenv("CONTENT_LENGTH");*/

	if (clen)
		len = strtol(clen, NULL, 10);
	else
		len = 0;

	if(len)
	{
		OutBuf buf;
		int i, ch, l;

		init_Buf(&buf);
		for (i = 0; i < len; i++)
		{
			if ((ch = FCGX_GetChar(in)) < 0)
			/*if ((ch = FCGI_fgetc(FCGI_stdin)) < 0)*/
				break;
			putByte_Buf(&buf, ch);
		}
		l = buf.ptr - buf.buf;
		putByte_Buf(&buf, 0);
		_clip_retcn_m(mp, buf.buf, l);
	}
	else
		_clip_retc(mp, "");

	return 0;
}
Ejemplo n.º 8
0
/*
 * Internal function to read a line
 * XXX: does not check for a valid stream,
 * the caller is responsible for that!
 *
 * - if bytesrequested is 0, an empty string is returned
 * - if bytesrequested > 0, it is treated as the max
 *   length of the line to return
 * - if bytesrequested < 0, an arbitrary length line
 *   is returned
 *
 */
static PyObject *
get_line(fcgi_Stream *self, long bytesrequested)
{
    FCGX_Stream *s;
    size_t bytesread, buffersize;
    PyObject *v;
    int c, done;

    s = *(self->s);

    if (bytesrequested == 0)
        return PyString_FromString("");

    if (bytesrequested < 0)
        buffersize = new_buffersize((size_t)0);
    else
        buffersize = bytesrequested;

    if (buffersize > INT_MAX) {
        PyErr_SetString(PyExc_OverflowError,
          "requested number of bytes is more than a Python string can hold");
        return NULL;
    }

    v = PyString_FromStringAndSize((char *)NULL, buffersize);
    if (v == NULL)
        return NULL;

    bytesread = 0;
    done = 0;

    for(;;) {
        Py_BEGIN_ALLOW_THREADS
        while (buffersize - bytesread > 0) {
            c = FCGX_GetChar(s);
            if (c == EOF) {
                if (bytesread == 0) {
                    Py_BLOCK_THREADS
                    Py_DECREF(v);
                    return PyString_FromString("");
                } else {
                    done = 1;
                    break;
                }
            }

            *(BUF(v) + bytesread) = (char) c;
            bytesread++;

            if (c == '\n') {
                done = 1;
                break;
            }
        }
        Py_END_ALLOW_THREADS
	if (done || (bytesread == bytesrequested))
            break;

        if (bytesrequested < 0) {
            buffersize = new_buffersize(buffersize);
            if (_PyString_Resize(&v, buffersize) < 0)
                return NULL;
        }
    }

    if (bytesread != buffersize)
        _PyString_Resize(&v, bytesread);

    return v;
}
Ejemplo n.º 9
0
int FastCgiDevice::getch()
{
	return FCGX_GetChar( m_stream ); 
}