static void InsertKeyValue( DaoProcess *proc, DaoMap *mulmap, DaoMap *map, DaoValue *vk, DaoValue *vv ) { DaoValue *val, *vlist; DaoMap_Insert( map, vk, vv ); if( mulmap ) { val = DaoMap_GetValue( mulmap, vk ); if( val == NULL ) { vlist = (DaoValue*) DaoProcess_NewList( proc ); DaoMap_Insert( mulmap, vk, vlist ); val = DaoMap_GetValue( mulmap, vk ); } DaoList_PushBack( DaoValue_CastList( val ), vv ); } }
static void ParseKeyValueStringArray( DaoProcess *proc, DaoMap *map, char **p ) { int nc = 0; char buffer[ LOCAL_BUF_SIZE + 1 ]; DaoValue *vk = (DaoValue*) DaoProcess_NewString( proc, NULL, 0 ); DaoValue *vv = (DaoValue*) DaoProcess_NewString( proc, NULL, 0 ); DString *key = DaoString_Get( DaoValue_CastString( vk ) ); DString *value = DaoString_Get( DaoValue_CastString( vv ) ); while( *p != NULL ) { char *c = *p; nc = 0; while( *c != '=' ) { if( nc >= LOCAL_BUF_SIZE ) { buffer[ nc ] = 0; DString_AppendChars( key, buffer ); nc = 0; } buffer[ nc ] = *c; nc ++; c ++; } buffer[ nc ] = 0; DString_AppendChars( key, buffer ); c ++; DString_AppendChars( value, c ); DaoMap_Insert( map, vk, vv ); DString_Clear( key ); DString_Clear( value ); p ++; } }
static void STD_Map( DaoProcess *proc, DaoValue *p[], int N ) { DaoInteger idint = {DAO_INTEGER,0,0,0,0,0}; DaoValue *res, *index = (DaoValue*)(void*)&idint; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoMap *map = DaoProcess_PutMap( proc, p[1]->xInteger.value ); daoint i, entry, size = p[0]->xInteger.value; if( sect == NULL || size < 0 ) return; // TODO exception if( DaoProcess_PushSectionFrame( proc ) == NULL ) return; entry = proc->topFrame->entry; DaoProcess_AcquireCV( proc ); for(i=0; i<size; i++){ idint.value = i; if( sect->b >0 ) DaoProcess_SetValue( proc, sect->a, index ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res->type == DAO_TUPLE && res->xTuple.size == 2 ) DaoMap_Insert( map, res->xTuple.items[0], res->xTuple.items[1] ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); }
static void DaoState_WaitFor2( DaoProcess *proc, DaoValue *p[], int N ) { DaoState *self = (DaoState*)DaoValue_CastCstruct( p[0], NULL ); int eq = 0, res = 1; DaoValue *state = p[1]; float timeout; DaoCondVar *condvar = NULL; DaoMutex_Lock( self->lock ); if( !DaoValue_Compare( self->state, state ) ) eq = 1; else{ condvar = (DaoCondVar*)DaoMap_GetValue( self->demands, state ); if( !condvar ){ condvar = DaoCondVar_New(); DaoMap_Insert( self->demands, state, (DaoValue*)condvar ); } } DaoMutex_Unlock( self->lock ); if( !eq ){ DaoMutex_Lock( self->defmtx ); timeout = DaoValue_TryGetFloat( p[2] ); if( timeout > 0 ) do res = !DaoCondVar_TimedWait( condvar, self->defmtx, timeout ); while( res && DaoValue_Compare( self->state, state ) ); else if( timeout == 0 ) res = 0; else do DaoCondVar_Wait( condvar, self->defmtx ); while( DaoValue_Compare( self->state, state ) ); DaoMutex_Unlock( self->defmtx ); } DaoProcess_PutInteger( proc, res ); }
static void SYS_EnvVars( DaoProcess *proc, DaoValue *p[], int N ) { #define LOCAL_BUF_SIZE 256 DaoMap *map = DaoProcess_PutMap( proc, 0 ); DaoValue *vk = (DaoValue*) DaoProcess_NewMBString( proc, NULL, 0 ); DaoValue *vv = (DaoValue*) DaoProcess_NewMBString( proc, NULL, 0 ); DString *key = DaoString_Get( DaoValue_CastString( vk ) ); DString *value = DaoString_Get( DaoValue_CastString( vv ) ); char **envs = environ; char buffer[ LOCAL_BUF_SIZE + 1 ]; int nc = 0; while( *envs != NULL ){ char *c = *envs; nc = 0; while( *c != '=' ){ if( nc >= LOCAL_BUF_SIZE ){ buffer[ nc ] = 0; DString_AppendMBS( key, buffer ); nc = 0; } buffer[ nc ] = *c; nc ++; c ++; } buffer[ nc ] = 0; DString_AppendMBS( key, buffer ); c ++; DString_AppendMBS( value, c ); DaoMap_Insert( map, vk, vv ); DString_Clear( key ); DString_Clear( value ); envs ++; } }
static DaoValue* DaoValue_DeepCopy( DaoValue *self ) { DNode *it; daoint i; if( self == NULL ) return NULL; if( self->type <= DAO_ENUM ) return self; /* simple types will be copied at use; */ if( self->type == DAO_ARRAY ) return (DaoValue*) DaoArray_Copy( (DaoArray*) self ); if( self->type == DAO_LIST ){ DaoList *list = (DaoList*) self; DaoList *copy = DaoList_New(); GC_ShiftRC( list->unitype, copy->unitype ); copy->unitype = list->unitype; for(i=0; i<list->items.size; ++i){ DaoValue *value = DaoValue_DeepCopy( list->items.items.pValue[i] ); DaoList_Append( copy, value ); } return (DaoValue*) copy; }else if( self->type == DAO_MAP ){ DaoMap *map = (DaoMap*) self; DaoMap *copy = DaoMap_New( map->items->hashing ); GC_ShiftRC( map->unitype, copy->unitype ); copy->unitype = map->unitype; for(it=DMap_First(map->items); it; it=DMap_Next(map->items,it)){ DaoValue *key = DaoValue_DeepCopy( it->key.pValue ); DaoValue *value = DaoValue_DeepCopy( it->value.pValue ); DaoMap_Insert( copy, key, value ); } return (DaoValue*) copy; }else if( self->type == DAO_TUPLE ){ DaoTuple *tuple = (DaoTuple*) self; DaoTuple *copy = DaoTuple_New( tuple->size ); GC_ShiftRC( tuple->unitype, copy->unitype ); copy->unitype = tuple->unitype; for(i=0; i<tuple->size; ++i){ DaoValue *value = DaoValue_DeepCopy( tuple->items[i] ); DaoTuple_SetItem( copy, value, i ); } return (DaoValue*) copy; } return NULL; }
static void PreparePostData( DaoProcess *proc, DaoMap *httpPOSTS, DaoMap *httpPOST, DaoMap *httpFILE ) { DString *fname; DString *buffer = DString_New(); DaoValue *vk = (DaoValue*) DaoProcess_NewString( proc, NULL, 0 ); DaoValue *vv = (DaoValue*) DaoProcess_NewString( proc, NULL, 0 ); DString *key = DaoString_Get( DaoValue_CastString( vk ) ); DString *value = DaoString_Get( DaoValue_CastString( vv ) ); char *content_length = getenv( "CONTENT_LENGTH" ); char *content_type = getenv( "CONTENT_TYPE" ); const char *boundary; char postbuf[1024]; daoint postlen, boundarylen, len = 0; daoint pos, pos2, pos_rnrn, offset; if( content_length != NULL ) len = strtol( content_length, NULL, 10); if( len == 0 ) return; DString_SetSharing( buffer, 0 ); if( content_type == NULL || strstr( content_type, "multipart/form-data" ) == NULL ) { postlen = fread( postbuf, 1, sizeof(postbuf), stdin ); while( postlen ) { DString_AppendBytes( buffer, postbuf, postlen ); postlen = fread( postbuf, 1, sizeof(postbuf), stdin ); } ParseKeyValueString( proc, httpPOSTS, httpPOST, buffer->chars ); DString_Delete( buffer ); return; } boundary = strstr( content_type, "boundary=" ) + strlen( "boundary=" ); boundarylen = strlen( boundary ); fname = DString_New(); buffer->size = 0; for(;;) { postlen = fread( postbuf, 1, sizeof(postbuf), stdin ); if( postlen == 0 && buffer->size < boundarylen ) break; DString_AppendBytes( buffer, postbuf, postlen ); while( strstr( buffer->chars, "\r\n\r\n" ) == 0 && postlen != 0 ) { postlen = fread( postbuf, 1, sizeof(postbuf), stdin ); DString_AppendBytes( buffer, postbuf, postlen ); } //printf( "###############\n%s\n", buffer->chars ); key->size = 0; fname->size = 0; pos = DString_FindChars( buffer, "name=", 20 ); /* Skip: Content-Disposition: ; */ pos2 = DString_FindChar( buffer, '\"', pos+6 ); DString_SubString( buffer, key, pos + 6, pos2 - pos - 6 ); pos_rnrn = DString_FindChars( buffer, "\r\n\r\n", pos2 ); pos = DString_FindChars( buffer, "filename=", pos2 ); if( pos != DAO_NULLPOS && pos < pos_rnrn ) { daoint pos3 = DString_FindChar( buffer, '\"', pos+10 ); DString_SubString( buffer, fname, pos + 10, pos3 - pos - 10 ); } buffer->size -= pos_rnrn + 4; memmove( buffer->chars, buffer->chars + pos_rnrn + 4, buffer->size ); if( fname->size == 0 ) { offset = 0; while( (pos2 = DString_FindChars( buffer, boundary, offset )) == DAO_NULLPOS ) { offset = buffer->size - boundarylen; if( offset < 0 ) offset = 0; postlen = fread( postbuf, 1, sizeof(postbuf), stdin ); DString_AppendBytes( buffer, postbuf, postlen ); } DString_SubString( buffer, value, 0, pos2 - 4 ); /* \r\n-- */ DaoMap_Insert( httpPOST, (DaoValue*) vk, (DaoValue*) vv ); buffer->size -= pos2 + boundarylen; memmove( buffer->chars, buffer->chars + pos2 + boundarylen, buffer->size ); } else { DaoInteger isize = {DAO_INTEGER,0,0,0,0,0}; DaoFileStream *stream = _DaoFileStream_New(); DaoTuple *tuple = DaoTuple_New(3); FILE *file = tmpfile(); DaoString_Set( (DaoString*) vv, fname ); stream->file = file; stream->base.mode |= DAO_STREAM_READABLE|DAO_STREAM_WRITABLE; _DaoFileStream_InitCallbacks( stream ); DaoTuple_SetType( tuple, daox_type_namestream ); DaoTuple_SetItem( tuple, (DaoValue*) vv, 0 ); DaoTuple_SetItem( tuple, (DaoValue*) stream, 2 ); DaoMap_Insert( httpFILE, (DaoValue*) vk, (DaoValue*) tuple ); offset = 0; while( (pos2 = DString_FindChars( buffer, boundary, 0 )) == DAO_NULLPOS ) { offset = buffer->size - boundarylen; if( offset > 0 ) { isize.value += offset; fwrite( buffer->chars, 1, offset, file ); buffer->size -= offset; memmove( buffer->chars, buffer->chars + offset, buffer->size ); } postlen = fread( postbuf, 1, sizeof(postbuf), stdin ); DString_AppendBytes( buffer, postbuf, postlen ); } isize.value += pos2 - 4; fwrite( buffer->chars, 1, pos2 - 4, file ); /* \r\n-- */ buffer->size -= pos2 + boundarylen; memmove( buffer->chars, buffer->chars + pos2 + boundarylen, buffer->size ); rewind( file ); DaoTuple_SetItem( tuple, (DaoValue*) & isize, 1 ); } } DString_Delete( buffer ); }
static void PreparePostData( DaoProcess *proc, DaoMap *httpPOSTS, DaoMap *httpPOST, DaoMap *httpFILE ) { DaoValue *vk = (DaoValue*) DaoProcess_NewMBString( proc, NULL, 0 ); DaoValue *vv = (DaoValue*) DaoProcess_NewMBString( proc, NULL, 0 ); DString *key = DaoString_Get( DaoValue_CastString( vk ) ); DString *value = DaoString_Get( DaoValue_CastString( vv ) ); DString *dynaBuffer = DString_New(1); int i = 0; int len = 0; char buffer[ LOCAL_BUF_SIZE + 1 ]; char *last = buffer + (LOCAL_BUF_SIZE-1); char *contentLength = getenv( "CONTENT_LENGTH" ); char *contentType = getenv( "CONTENT_TYPE" ); len = 0; *last = 0; if( contentLength != NULL ) len = strtol( contentLength, NULL, 10); if( contentType != NULL ){ //printf( "CONTENT_TYPE = %s\n", contentType ); if( strstr( contentType, "multipart/form-data" ) == NULL ){ i = 0; DString_Clear( dynaBuffer ); while( i < len ){ int n = 0; int ch = getchar(); while( ch != EOF ){ buffer[n] = (char)ch; n ++; if( n == LOCAL_BUF_SIZE ) break; ch = getchar(); } buffer[ n ] = 0; //printf( "%s|||||||||||||||||\n", buffer ); char *p = strchr( buffer, '&' ); if( p != NULL ){ *p = 0; // null-terminating p++; DString_AppendMBS( dynaBuffer, buffer ); ParseKeyValueString( proc, httpPOSTS, httpPOST, DString_GetMBS( dynaBuffer ) ); DString_Clear( dynaBuffer ); DString_AppendMBS( dynaBuffer, p ); }else{ DString_AppendMBS( dynaBuffer, buffer ); } i += LOCAL_BUF_SIZE; } ParseKeyValueString( proc, httpPOSTS, httpPOST, DString_GetMBS( dynaBuffer ) ); }else{ char *boundary = strstr( contentType, "boundary" ); boundary = strstr( boundary, "=" ) + 1; i = 0; char *part = NULL; while( ! feof( stdin ) ){ if( part == NULL ) part = fgets( buffer, LOCAL_BUF_SIZE, stdin ); if( part == NULL ) break; if( strstr( part, boundary ) == NULL ) break; // read content information DString_Clear( dynaBuffer ); buffer[ LOCAL_BUF_SIZE ] = 0; // null-terminating char *p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); if( p == NULL ) break; // the last boundary scanned p = strchr( p, '\n' ); *p = 0; // null-terminating DString_AppendMBS( dynaBuffer, buffer ); char *info = (char*)DString_GetMBS( dynaBuffer ); info = strchr( info, '=' ); info += 2; // at char after name=" p = info; while( *p != '\"' ) p ++; *p = 0; // null-terminating DString_SetMBS( key, info ); p ++; if( (p = strstr(p,"filename") ) == NULL ){ p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); // now real data: DString_Clear( value ); while( p != NULL && strstr( p, boundary ) == NULL ){ char *t = strstr( p, "\r\n" ); if( t != NULL ) *t = 0; DString_AppendMBS( value, buffer ); if( feof( stdin ) ) break; p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); t = strchr( p, '\n' ); if( t!= NULL ) *(t+1) = 0; } if( p != NULL ) part = p; DaoMap_Insert( httpPOST, vk, vv ); }else{ DaoValue *vs = (DaoValue*) DaoProcess_NewStream( proc, tmpfile() ); FILE *file = DaoStream_GetFile( DaoValue_CastStream( vs ) ); char *t = NULL; p = strchr( p, '\"' ) + 1; info = p; while( *p != '\"' ) p ++; *p = 0; // null-terminating //XXX stream->TYPER->SetName( stream, info ); DString_Clear( value ); // Content-Type ...\r\n p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); // \r\n p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); // data #if 0 int count = fread( buffer, 1, LOCAL_BUF_SIZE, stdin ); while( count && strstr( buffer, boundary ) == NULL ){ fwrite( buffer, 1, count, file ); fprintf( file, "%s\n", "===========================" ); if( feof( stdin ) ) break; count = fread( buffer, 1, LOCAL_BUF_SIZE, stdin ); } #else char tail[3] = { 0, 0, 0 }; int count, ntail = 0; p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); while( p != NULL && strstr( p, boundary ) == NULL ){ if( feof( stdin ) ){ // XXX break; }else{ t = p; while( t != last && (*t) != '\n' ) t ++; if( (*t) == '\n' ){ count = t-p+1; if( count >= 2 ){ count -= 2; if( ntail ) fwrite( tail, 1, ntail, file ); tail[0] = p[ count ]; tail[1] = p[ count+1 ]; ntail = 2; fwrite( p, 1, count, file ); }else if( count == 1 ){ if( ntail == 2 ){ fwrite( tail, 1, 1, file ); tail[0] = tail[1]; tail[1] = p[0]; }else if( ntail ==1 ){ tail[1] = p[0]; ntail = 2; }else{ tail[0] = p[0]; ntail = 1; } } }else{ if( ntail ) fwrite( tail, 1, ntail, file ); count = LOCAL_BUF_SIZE-3; tail[0] = p[ count ]; tail[1] = p[ count+1 ]; ntail = 2; fwrite( p, 1, count, file ); } } p = fgets( buffer, LOCAL_BUF_SIZE, stdin ); } #endif //if( p != NULL ) part = p; rewind( file ); DaoMap_Insert( httpFILE, vk, vs ); } } } } DString_Delete( dynaBuffer ); }