📄 sbineturl.cpp
字号:
QueryArgsMapToHtList(arglist, (const VXIMap *)value, fieldName); // Parse map } else if(VXIValueGetType(value) == VALUE_VECTOR) { // nested vector VXIchar fieldName[1024]; wcscpy(fieldName, key); QueryArgsVectorToHtList(arglist, (const VXIVector *)value, fieldName); // Parse vector } else { const VXIchar* valStr = ValueToString(value, tempBuf, 2048); AddObjectToHTAssocList( arglist, key, valStr); } } } while ( VXIMapGetNextProperty( mapIterator, &key, &value ) == VXIvalue_RESULT_SUCCESS ); VXIMapIteratorDestroy(&mapIterator); return(arglist);}/* * Utilities */void SBinetURL::AppendKeyToMultipart( const VXIchar *key ){ char tempCBuf[2048]; char tempKey[2048]; int len; len = ::wcslen(key); if(len > 2047) len = 2047; // should realloc but... ::wcstombs( tempKey, key, len ); tempKey[len] = 0; // Print the boundary start sprintf(tempCBuf,"--%s%s", SB_BOUNDARY, CRLF); appendStr(tempCBuf); sprintf(tempCBuf, "Content-Disposition: form-data; name=\"%s\"%s", tempKey, CRLF); appendStr(tempCBuf);}void SBinetURL::AppendStringValueToMultipart( const VXIchar *value ){ char tempCBuf[2048]; char tempVal[2048]; int len; if(value){ len = ::wcslen(value); if(len > 2047) len = 2047; // should realloc but... ::wcstombs( tempVal, value, len ); tempVal[len] = 0; // I am guessing what headers should be sprintf(tempCBuf,"Content-Type: text/plain%s", CRLF); appendStr(tempCBuf); sprintf(tempCBuf,"Content-Length: %d%s", strlen(tempVal), CRLF); appendStr(tempCBuf); sprintf(tempCBuf,"%s%s%s", CRLF, tempVal, CRLF); // One blank line before appendStr(tempCBuf); }}void SBinetURL::QueryArgsVectorToMultipart( const VXIVector *vector, VXIchar *fieldName ){ const VXIValue *value = NULL; VXIunsigned vectorLen = VXIVectorLength(vector); VXIunsigned i; for(i = 0 ; i < vectorLen ; i++) { value = VXIVectorGetElement(vector, i); if ( value != NULL ){ VXIunsigned prevLen = ::wcslen(fieldName); AppendArrayIndexToName(fieldName, i); if(VXIValueGetType(value) == VALUE_MAP) { // nested object // Append the object name to the field name prefix QueryArgsMapToMultipart((const VXIMap *)value, fieldName); // Parse map } else if(VXIValueGetType(value) == VALUE_VECTOR) { // nested vector // Append the vector name to the field name prefix QueryArgsVectorToMultipart((const VXIVector *)value, fieldName); // Recursive } else { VXIchar tempBuf[2048]; const VXIchar* valStr = ValueToString(value, tempBuf, 2048); AppendKeyToMultipart(fieldName); AppendStringValueToMultipart(valStr); } fieldName[prevLen] = L'\0'; // Remove the array index } }}void SBinetURL::QueryArgsMapToMultipart( const VXIMap *map, VXIchar *fieldName ){ const VXIchar *key = NULL; const VXIValue *value = NULL; VXIMapIterator *mapIterator = VXIMapGetFirstProperty( map, &key, &value ); do{ if (( key != NULL ) && ( value != NULL )){ if(VXIValueGetType(value) == VALUE_MAP) { // nested object // Append the object name to the field name prefix if( fieldName[0] != L'\0' ) wcscat(fieldName, L"."); wcscat(fieldName, key); QueryArgsMapToMultipart((const VXIMap *)value, fieldName); // Recursive } else if(VXIValueGetType(value) == VALUE_VECTOR) { // nested vector // Append the object name to the field name prefix if( fieldName[0] != L'\0' ) wcscat(fieldName, L"."); wcscat(fieldName, key); QueryArgsVectorToMultipart((const VXIVector *)value, fieldName); // Parse vector } else { VXIchar tempBuf[2048]; // Append the real field name to the field name prefix VXIint fieldNameLen = ::wcslen(fieldName); if( fieldName[0] != L'\0' ) wcscat(fieldName, L"."); wcscat(fieldName, key); const VXIchar* valStr = ValueToString(value, tempBuf, 2048); AppendKeyToMultipart(fieldName); AppendStringValueToMultipart(valStr); // Remove the just appended file name, leave only the prefix fieldName[fieldNameLen] = L'\0'; } } } while ( VXIMapGetNextProperty( mapIterator, &key, &value ) == VXIvalue_RESULT_SUCCESS ); // Cut the last object name from the field name prefix VXIchar *fieldNamePtr = wcsrchr(fieldName, L'.'); if( fieldNamePtr != NULL ) *fieldNamePtr = L'\0'; else fieldName[0] = L'\0'; VXIMapIteratorDestroy(&mapIterator);}const char*SBinetURL::QueryArgsToMultipart(const VXIMap* queryArgs,VXIulong* plen){ // Create a list to hold the form arguments, then parse the // data and add the key-value pairs to the association list const VXIchar *key = NULL; const VXIValue *value = NULL; char tempKey[2048]; const VXIchar* valStr; int len; if(!queryArgs) return(NULL); VXIMapIterator *mapIterator = VXIMapGetFirstProperty( queryArgs, &key, &value ); // First go through and compute lengths int totalLen = 0; do{ if (( key != NULL ) && ( value != NULL )) { len = ::wcslen(key); if(len > 2047) len = 2047; // should realloc but... ::wcstombs( tempKey, key, len ); tempKey[len] = 0; if(VXIValueGetType(value) == VALUE_CONTENT){ // audio const VXIchar* type; const VXIbyte* data; VXIulong size; VXIContentValue((const VXIContent *)value,&type,&data,&size); totalLen += size; } else{ // normal stuff // TODO: We should compute the size for maps and vectors !!! totalLen += 2048; // bogus max value length. } } } while ( VXIMapGetNextProperty( mapIterator, &key, &value ) == VXIvalue_RESULT_SUCCESS ); VXIMapIteratorDestroy(&mapIterator); initDoc(totalLen + 2048); // extra for headers mapIterator = VXIMapGetFirstProperty( queryArgs, &key, &value ); char tempCBuf[2048]; // do{ if (( key != NULL ) && ( value != NULL )) { if(VXIValueGetType(value) == VALUE_CONTENT){ // audio const VXIchar* type; const VXIbyte* data; VXIulong size; AppendKeyToMultipart(key); VXIContentValue((const VXIContent *)value, &type, &data, &size); // I am guessing what headers should be sprintf(tempCBuf,"Content-Type: %s%s", type, CRLF); appendStr(tempCBuf); sprintf(tempCBuf,"Content-Length: %d%s", size, CRLF); appendStr(tempCBuf); sprintf(tempCBuf,"%s", CRLF); // One blank line appendStr(tempCBuf); appendData((char*)data, size); } else if(VXIValueGetType(value) == VALUE_MAP){ // nested object VXIchar fieldName[1024]; wcscpy(fieldName, key); QueryArgsMapToMultipart((const VXIMap *)value, fieldName); // Parse map } else if(VXIValueGetType(value) == VALUE_VECTOR){ // nested vector VXIchar fieldName[1024]; wcscpy(fieldName, key); QueryArgsVectorToMultipart((const VXIVector *)value, fieldName); // Parse vector } else{ // normal stuff VXIchar tempBuf[2048]; valStr = ValueToString(value,tempBuf, 2048); // either tempBuf or VXIString contents AppendKeyToMultipart(key); AppendStringValueToMultipart(valStr); } } } while ( VXIMapGetNextProperty( mapIterator, &key, &value ) == VXIvalue_RESULT_SUCCESS ); // Print the boundary terminator sprintf(tempCBuf,"--%s--%s", SB_BOUNDARY, CRLF); appendStr(tempCBuf); VXIMapIteratorDestroy(&mapIterator); *plen = m_doc.length(); return(m_doc.c_str());}VXIinetResult SBinetURL::WideToNarrowString(const VXIchar* pszWide, SBinetNString& strNarrow){ VXIinetResult eResult( VXIinet_RESULT_SUCCESS ); strNarrow = ""; if (pszWide == NULL) return VXIinet_RESULT_INVALID_ARGUMENT; size_t nCharCount( ::wcslen( pszWide ) ); for (size_t i=0; i < nCharCount; i++) { if ( pszWide[i] < CHAR_MIN || pszWide[i] > CHAR_MAX) eResult = VXIinet_RESULT_INVALID_ARGUMENT; strNarrow += (char)pszWide[i]; } return eResult;}VXIinetResult SBinetURL::NarrowToWideString(const char* pszNarrow, SBinetString& strWide){ VXIinetResult eResult( VXIinet_RESULT_SUCCESS ); strWide = L""; if (pszNarrow == NULL) return VXIinet_RESULT_INVALID_ARGUMENT; size_t nCharCount( ::strlen( pszNarrow ) ); for (size_t i=0; i < nCharCount; i++) { strWide += (wchar_t)pszNarrow[i]; } return eResult;}//// Infer a MIME content type from a URL (by the extension)//VXIinetResult SBinetURL::ContentTypeFromUrl(SBinetString* strContentType) const{ if(!strContentType) return VXIinet_RESULT_INVALID_ARGUMENT; // // Determine the extension. This has to work for local files and also HTTP // const VXIchar *url = m_strAbsoluteUrl.c_str(); const VXIchar *urlEnd = url + ::wcslen(url); if((url == NULL) || (url == urlEnd)) return VXIinet_RESULT_FAILURE; // Don't look farther than the first '?' (query args) or '#' const VXIchar *urlQueryArgs = wcschr(url, L'?'); const VXIchar *urlHash = wcschr(url, L'#'); if(urlQueryArgs) { if(urlHash && (urlHash < urlQueryArgs)) urlEnd = urlHash; else urlEnd = urlQueryArgs; } else if(urlHash) urlEnd = urlHash; // Now look for the last '.' VXIchar ext[1024] = L""; const VXIchar *urlDot = urlEnd - 1; while(urlDot >= url) { if(*urlDot == L'.') break; urlDot--; } if(urlDot >= url) { ::wcsncpy(ext, urlDot, urlEnd - urlDot); ext[urlEnd - urlDot] = L'\0'; } if(ext[0] != L'\0') { const VXIString* typeFromMap = SBinetInterface::mapExtension(ext); if(typeFromMap){ *strContentType = VXIStringCStr(typeFromMap); return(VXIinet_RESULT_SUCCESS); } } /* * Could not find in map, use Win32 if available */#ifdef WIN32 // Try Windows, this supports a number of hardcoded MIME content // types as well as extension mapping rules. To define a new // extension mapping rule, create a new Win32 registry key called // HKEY_CLASSES_ROOT\.<ext> where <ext> is the extension name. Then // create a value under that called "Content Type" where the data is // the content type string, such as "audio/aiff". Browse that // location in the registry for numerous examples (not all have a // content type mapping though). // // TBD sniff the actual data buffer too as this permits, pass the // proposed MIME type too (as returned by the web server, if any). VXIchar *mimeBuf; if (( FindMimeFromData (NULL, url, NULL, 0, NULL, 0, &mimeBuf, 0) == NOERROR ) && mimeBuf && mimeBuf[0] ) { *strContentType = mimeBuf; return VXIinet_RESULT_SUCCESS; }#endif /* * Couldn't figure out MIME type, set to default. */ *strContentType = DEFAULT_GENERIC_MIME_TYPE; return VXIinet_RESULT_FAILURE; // Should we return success??}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -