예제 #1
0
/** \brief compare 2 objects (ala memcmp) - as in rfc2616.3.2.3
 * 
 * - it return a value <  0 if the local object is less than the external one
 * - it return a value == 0 if the local object is equal to the external one
 * - it return a value >  0 if the local object is greater than the external one
 */
int http_sresp_ctx_t::compare(const http_sresp_ctx_t & other)  const throw()
{
	// handle the case where at least one is null
	if(  is_null() && !other.is_null() )	return -1;
	if( !is_null() &&  other.is_null() )	return +1;
	if(  is_null() &&  other.is_null() )	return  0;
	// NOTE: here both are NOT null
	
	// compare the reqhd
	if( reqhd()	< other.reqhd() )	return -1;
	if( reqhd()	> other.reqhd() )	return +1;

	// compare the rephd
	if( rephd()	< other.rephd() )	return -1;
	if( rephd()	> other.rephd() )	return +1;

	// compare the post_data
	if( post_data()	< other.post_data() )	return -1;
	if( post_data()	> other.post_data() )	return +1;

	// compare the response_body
	if( response_body().str()	< other.response_body().str() )	return -1;
	if( response_body().str()	> other.response_body().str() )	return +1;
	
	// note: here both are considered equal
	return 0;
}
/**
 * Build HTTP response
 */
std::string HttpServer::build_response(std::string urlpath)
{
    int status_code;

    std::string msg = response_body(urlpath, status_code);
    std::string message = "HTTP/1.0 ";
    message += std::to_string(status_code);

    switch(status_code)
    {
        case HTTP_OK:
            message += " OK\r\n";
            break;
        case HTTP_NOT_FOUND:
            message += " Not Found\r\n";
            break;
        default:
            message += " What?\r\n";
    }

    message += "Server: Handmade C++ HTTP Server\r\n";
    message += "Content-Type: text/html\r\n";
    message += "Content-Length: ";
    message += std::to_string(msg.length());
    message += "\r\n";
    message += "\r\n";
    message += msg;
    message += "\r\n";

    return message;
}
예제 #3
0
/** \brief convert the object into a string
 */
std::string http_sresp_ctx_t::to_string()			const throw()
{
	std::ostringstream	oss;
	// handle the null case
	if( is_null() )	return "null";
	// build the string
	oss << "[";
	oss << "reqhd="			<< reqhd();
	oss << " rephd="		<< rephd();
	oss << " post_data="		<< post_data();
	oss << " response_body="	<< response_body().str();
	oss << "]";
	// return the just built string
	return oss.str();
}
예제 #4
0
파일: myengine.cpp 프로젝트: biedro/s2c
void MyEngine::play() {
    qDebug() << "> Play()";
    //// move to different class
    //Phonon::MediaObject *music =
    //        Phonon::createPlayer(Phonon::MusicCategory,
     //                            Phonon::MediaSource("/path/mysong.mp3"));
    //music->play();

    QByteArray response_body("\"{\"status\":0,\"id\":\"debc3120d3cfe2e60fc43505b184883c-1\",\"hypotheses\":[{\"utterance\":\"system\",\"confidence\":0.6186791}]}");


    if (response_body.count() == 0) {
        qDebug() << "Response was empty.";
    }
    qDebug() << "Parsing response " << response_body;

    // Parse the response, ignoring comments.

    QScriptValue sc;
    QScriptEngine engine;
    sc = engine.evaluate(QString(response_body)); // In new versions it may need to look like engine.evaluate("(" + QString(result) + ")");

    //{"status":0,"id":"421d36084c097778b2f81d60caef6d99-1","hypotheses":[{"utterance":"system","confidence":0.6186791}]}

    qDebug() << kHypothesesStr << " -> " << sc.property(kHypothesesStr).toString();
    qDebug() << kUtteranceStr << " -> " << sc.property(kUtteranceStr).toString();
    qDebug() << kConfidenceStr << " -> " << sc.property(kConfidenceStr).toString();

    if (sc.property(kHypothesesStr).isArray())
    {
        qDebug() << "jestem w domu";

        QStringList items;
        qScriptValueToSequence(sc.property(kHypothesesStr), items);

        foreach (QString str, items) {
            qDebug("value %s",str.toStdString().c_str());
        }
예제 #5
0
/* The server has encountered an error. Let the client know and add it to the logs. */
static int server_error(int client_fd, int err_code)
{
	char err_msg[32];
	switch (err_code) {
		case 400: strcpy(err_msg,"Bad Request"); break;
		case 404: strcpy(err_msg,"File Not Found"); break;
		case 500: strcpy(err_msg,"Internal Server Error"); break;
	}
		
	main_log(std::string("Couldn't handle client request: ") + std::to_string(err_code) + err_msg);
	std::string status_line;
	std::string response_body("<html><head><title>");
	response_body += std::to_string(err_code) + " " + err_msg + "</title></head>";
	response_body += "<body><h1>" + std::to_string(err_code) + " " + err_msg + "</h1></body></html>";
	status_line += "HTTP/1.1 " + std::to_string(err_code) + err_msg + "\r\n" +
			"Content-Length: " + std::to_string(response_body.size()) + "\r\n" +
			"Connection: close\r\n" +
			"\r\n" +
			response_body + "\r\n";
	write(client_fd,status_line.c_str(),status_line.size());
	main_log("Sent:\n" + status_line);
	return err_code;
}
예제 #6
0
ssize_t ResponseMessage::decode(char* input, size_t size) {
  char* input_pos = input;

  received_ += size;

  if (!is_header_received_) {
    if (version_ == 0) {
      version_ = input[0] & 0x7F; // "input" will always have at least 1 bytes
      if (version_ >= 3) {
        header_size_  = CASS_HEADER_SIZE_V3;
      } else {
        header_size_ = CASS_HEADER_SIZE_V1_AND_V2;
      }
    }

    if (received_ >= header_size_) {
      // We may have received more data then we need, only copy what we need
      size_t overage = received_ - header_size_;
      size_t needed = size - overage;

      memcpy(header_buffer_pos_, input_pos, needed);
      header_buffer_pos_ += needed;
      input_pos += needed;
      assert(header_buffer_pos_ == header_buffer_ + header_size_);

      char* buffer = header_buffer_ + 1; // Skip over "version" byte
      flags_ = *(buffer++);

      if (version_ >= 3) {
        buffer = decode_int16(buffer, stream_);
      } else {
        stream_ = *(buffer++);
      }
      opcode_ = *(buffer++);

      decode_int32(buffer, length_);

      is_header_received_ = true;

      if (!allocate_body(opcode_) || !response_body_) {
        return -1;
      }

      response_body_->set_buffer(length_);
      body_buffer_pos_ = response_body_->data();
    } else {
      // We haven't received all the data for the header. We consume the
      // entire buffer.
      memcpy(header_buffer_pos_, input_pos, size);
      header_buffer_pos_ += size;
      return size;
    }
  }

  const size_t remaining = size - (input_pos - input);
  const size_t frame_size = header_size_ + length_;

  if (received_ >= frame_size) {
    // We may have received more data then we need, only copy what we need
    size_t overage = received_ - frame_size;
    size_t needed = remaining - overage;

    memcpy(body_buffer_pos_, input_pos, needed);
    body_buffer_pos_ += needed;
    input_pos += needed;
    assert(body_buffer_pos_ == response_body_->data() + length_);

    char* pos = response_body()->data();

    if (flags_ & CASS_FLAG_WARNING) {
      pos = response_body()->decode_warnings(pos, length_);
    }

    if (flags_ & CASS_FLAG_CUSTOM_PAYLOAD) {
      pos = response_body()->decode_custom_payload(pos, length_);
    }

    if (!response_body_->decode(version_, pos, length_)) {
      is_body_error_ = true;
      return -1;
    }

    is_body_ready_ = true;
  } else {
    // We haven't received all the data for the frame. We consume the entire
    // buffer.
    memcpy(body_buffer_pos_, input_pos, remaining);
    body_buffer_pos_ += remaining;
    return size;
  }

  return input_pos - input;
}