Beispiel #1
0
    void SimpleHttpClient::fillRequestBuffer (stringstream &requestBuffer, 
            int method,
            const std::string& location, 
            const char* body, 
            size_t bodyLength,
            const map<string, string>& headerFields) {

      switch (method) {
        case GET:
          requestBuffer << "GET ";
          break;
        case POST:
          requestBuffer << "POST ";
          break;
        case PUT:
          requestBuffer << "PUT ";
          break;
        case DELETE:
          requestBuffer << "DELETE ";
          break;
        case HEAD:
          requestBuffer << "HEAD ";
          break;
        default:
          requestBuffer << "POST ";
          break;
      }

      if (location.length() == 0 || location[0] != '/') {
        requestBuffer << "/";
      }

      requestBuffer << location << " HTTP/1.1\r\n";      
      
      requestBuffer << "Host: ";
      if (_connection->getHostname().find(':') != string::npos) {
        requestBuffer << "[" << _connection->getHostname() << "]";        
      }
      else {
        requestBuffer << _connection->getHostname();        
      }
      requestBuffer << ':' << _connection->getPort() << "\r\n";
      requestBuffer << "Connection: Keep-Alive\r\n";
      requestBuffer << "User-Agent: VOC-Client/1.0\r\n";
      
      if (bodyLength > 0) {
        requestBuffer << "Content-Type: application/json; charset=utf-8\r\n";
      }

      for (map<string, string>::const_iterator i = headerFields.begin(); i != headerFields.end(); ++i) {
        requestBuffer << i->first << ": " << i->second << "\r\n";
      }

      requestBuffer << "Content-Length: " << bodyLength << "\r\n\r\n";

      requestBuffer.write(body, bodyLength);
    }
void MySQLDatabase::EscapeLongString(const char* str, uint32 len, stringstream & out)
{
	char a2[65536 * 3] = { 0 };

	DatabaseConnection* con = GetFreeConnection();
	const char* ret;
	if(mysql_real_escape_string(static_cast<MySQLDatabaseConnection*>(con)->MySql, a2, str, (unsigned long)len) == 0)
		ret = str;
	else
		ret = a2;

	out.write(a2, (std::streamsize)strlen(a2));
	con->Busy.Release();
}
void PostgresDatabase::EscapeLongString(const char * str, uint32 len, stringstream& out)
{
	char a2[65536*3] = {0};

	DatabaseConnection * con = GetFreeConnection();
	const char * ret;
	int err;
	if(PQescapeStringConn(static_cast<PostgresDatabaseConnection*>(con)->PgSql, a2, str, (unsigned long)len, &err) == 0)
		ret = str;
	else
		ret = a2;

	out.write(a2, (std::streamsize)strlen(a2));
	con->Busy.Release();
}
int HumdrumFileBase::getFixedDataSize(int socket_id, int datalength,
		stringstream& inputdata, char* buffer, int bufsize) {
	int readcount = 0;
	int readsize;
	int message_len;

	while (readcount < datalength) {
		readsize = bufsize;
		if (readcount + readsize > datalength) {
			readsize = datalength - readcount;
		}
		message_len = ::read(socket_id, buffer, readsize);
		if (message_len == 0) {
			// shouldn't happen, but who knows...
			break;
		}
		inputdata.write(buffer, message_len);
		readcount += message_len;
	}

	return readcount;
}
void HumdrumFileBase::readStringFromHttpUri(stringstream& inputdata,
		const string& webaddress) {
	auto css = webaddress.find("://");
	if (css == string::npos) {
		// give up since URI was not in correct format
	}
	string rest = webaddress.substr(css+3);
	string hostname;
	string location;
	css = rest.find("/");
	if (css != string::npos) {
		hostname = rest.substr(0, css);
		location = rest.substr(css);
	} else {
		hostname = rest;
		location = "/";
	}
	if (location.empty()) {
		location = "/";
	}

	string newline({0x0d, 0x0a});

	stringstream request;
	request << "GET "   << location << " HTTP/1.1" << newline;
	request << "Host: " << hostname << newline;
	request << "User-Agent: HumdrumFile Downloader 2.0 ("
		     << __DATE__ << ")" << newline;
	request << "Connection: close" << newline;  // this line is necessary
	request << newline;

	unsigned short int port = 80;
	int socket_id = open_network_socket(hostname, port);
	if (::write(socket_id, request.str().c_str(), request.str().size()) == -1) {
		exit(-1);
	}

	#define URI_BUFFER_SIZE (10000)
	char buffer[URI_BUFFER_SIZE];
	int message_len;
	stringstream header;
	int foundcontent   = 0;
	int newlinecounter = 0;
	int i;

	// read the response header:
	while ((message_len = ::read(socket_id, buffer, 1)) != 0) {
		header << buffer[0];
		if ((buffer[0] == 0x0a) || (buffer[0] == 0x0d)) {
					newlinecounter++;
		} else {
					newlinecounter = 0;
		}
		if (newlinecounter == 4) {
			foundcontent = 1;
			break;
		}
	}
	if (foundcontent == 0) {
		cerr << "Funny error trying to read server response" << endl;
		exit(1);
	}

	// now read the size of the rest of the data which is expected
	int datalength = -1;

	// also, check for chunked transfer encoding:

	int chunked = 0;

	header << ends; // necessary?
	while (header.getline(buffer, URI_BUFFER_SIZE)) {
		int len = strlen(buffer);
		for (i=0; i<len; i++) {
			buffer[i] = std::tolower(buffer[i]);
		}
		if (strstr(buffer, "content-length") != NULL) {
			for (i=14; i<len; i++) {
				if (std::isdigit(buffer[i])) {
					sscanf(&buffer[i], "%d", &datalength);
					if (datalength == 0) {
						cerr << "Error: no data found for URI, probably invalid\n";
						cerr << "URL:   " << webaddress << endl;
						exit(1);
					}
					break;
				}
			}
		} else if ((strstr(buffer, "transfer-encoding") != NULL) &&
			(strstr(buffer, "chunked") != NULL)) {
			chunked = 1;
		}
	}

	// once the length of the remaining data is known (or not), read it:
	if (datalength > 0) {
		getFixedDataSize(socket_id, datalength, inputdata, buffer,
				URI_BUFFER_SIZE);

	} else if (chunked) {
		int chunksize;
		int totalsize = 0;
		do {
			chunksize = getChunk(socket_id, inputdata, buffer, URI_BUFFER_SIZE);
			totalsize += chunksize;
		} while (chunksize > 0);
		if (totalsize == 0) {
			cerr << "Error: no data found for URI (probably invalid)\n";
			exit(1);
		}
	} else {
		// if the size of the rest of the data cannot be found in the
		// header, then just keep reading until done (but this will
		// probably cause a 5 second delay at the last read).
		while ((message_len = ::read(socket_id, buffer, URI_BUFFER_SIZE)) != 0) {
			if (foundcontent) {
				inputdata.write(buffer, message_len);
			} else {
				for (i=0; i<message_len; i++) {
					if (foundcontent) {
						inputdata << buffer[i];
					} else {
						header << buffer[i];
						if ((buffer[i] == 0x0a) || (buffer[i] == 0x0d)) {
							newlinecounter++;
						} else {
							newlinecounter = 0;
						}
						if (newlinecounter == 4) {
							foundcontent = 1;
							continue;
						}
					}

				}
			}
		}
	}

	close(socket_id);
}
Beispiel #6
0
void CTiffWriter::GetImageHeader(stringstream &Header)
{
  // write the header
  TIFFIFH hdr = {0x4949, 0x002a, sizeof(TIFFIFH)};
  Header.write(reinterpret_cast<char*>(&hdr), sizeof(TIFFIFH));

  // write the Tags immediately after the header
  WORD numTags = 12;
  Header.write(reinterpret_cast<char*>(&numTags), sizeof(numTags));

  const int nsize = sizeof(TIFFTag);

  Header.write(reinterpret_cast<char*>(&m_ImageWidth), nsize);
  Header.write(reinterpret_cast<char*>(&m_ImageLength), nsize);
  Header.write(reinterpret_cast<char*>(&m_BitsPerSample), nsize);
  Header.write(reinterpret_cast<char*>(&m_Compression), nsize);
  Header.write(reinterpret_cast<char*>(&m_PhotometricInterp), nsize);
  Header.write(reinterpret_cast<char*>(&m_StripOffsets), nsize);
  Header.write(reinterpret_cast<char*>(&m_SamplesPerPixel), nsize);
  Header.write(reinterpret_cast<char*>(&m_RowsPerStrip), nsize);
  Header.write(reinterpret_cast<char*>(&m_StripByteCounts), nsize);
  Header.write(reinterpret_cast<char*>(&m_XResolution), nsize);
  Header.write(reinterpret_cast<char*>(&m_YResolution), nsize);
  Header.write(reinterpret_cast<char*>(&m_ResolutionUnit), nsize);

  // end the header by setting the next image offset to null
  DWORD end = 0;
  Header.write(reinterpret_cast<char*>(&end), sizeof(end));

  // write the X and Y resolutions
  Header.write(reinterpret_cast<char*>(&m_xres), sizeof(DWORD)*2);

  Header.write(reinterpret_cast<char*>(&m_yres), sizeof(DWORD)*2);
}
        uint32_t QueryableNetstringsDeserializerABCF::fillBuffer(istream& in, stringstream& buffer) {
            char _c = 0;
            uint32_t bytesRead = 0;

            // Peek Container's header: 2 bytes (0xABCF) followed by maximum 8 bytes (uint64_t) length of payload information.
            uint32_t bytesToRead = sizeof(uint16_t) + sizeof(uint64_t);

            // Read Container header + size of payload.
            while (in.good() && (bytesToRead > 0)) {
                // Read next byte.
                in.read(&_c, sizeof(char));
                bytesToRead--; bytesRead++;

                // Add new byte at the end of the buffer.
                buffer.write(&_c, sizeof(char));
            }

            // Decode Container's magic number.
            uint16_t magicNumber = 0;
            buffer.read(reinterpret_cast<char*>(&magicNumber), sizeof(uint16_t));
            magicNumber = ntohs(magicNumber);

            if (magicNumber == 0xABCF) {
                // Decode size of payload (encoded as varint).
                uint64_t value = 0;
                uint8_t size = 0;
                {
                    while (buffer.good()) {
                        char c = 0;
                        buffer.read(&c, sizeof(char));
                        value |= static_cast<unsigned int>( (c & 0x7f) << (0x7 * size++) );
                        if ( !(c & 0x80) ) break;
                    }
                    // Decode as little endian like in Protobuf's case.
                    value = le64toh(value);
                }

                bytesToRead = (value // This is the length of the payload.
                               - (sizeof(uint64_t) // We have already read this amount of bytes while peeking the header.
                                  - size) // This amount of bytes was consumed to encode the length of the payload in the varint field.
                                 )
                               + 1; // And we need to consume the final ','.

                // Add the data at the end of the buffer.
                buffer.seekg(0, ios_base::end);

                // Read remainder of the container.
                while (in.good() && (bytesToRead > 0)) {
                    // Read next byte.
                    in.read(&_c, sizeof(char));
                    bytesToRead--; bytesRead++;

                    // Add new byte at the end of the buffer.
                    buffer.write(&_c, sizeof(char));
                }

                // Move read pointer to the beginning to read container.
                buffer.seekg(0, ios_base::beg);
            }

            return bytesRead;
        }
void SQLiteDatabase::EscapeLongString(const char* str, uint32 len, stringstream& out)
{
	char a2[DATABASE_QUERY_LONG_BUFFER_SIZE] = {0};
	sqlite3_snprintf( DATABASE_QUERY_LONG_BUFFER_SIZE, a2, str );
	out.write(a2, (std::streamsize)strlen(a2));
}