示例#1
0
int TcpReader::handle(const char * data, size_t length, Connection& )
{
  int bytesRemaining = (int)length;
  int payloadSize = payloadLength( data, bytesRemaining );
  int bytesHandled = 0;
  while( payloadIsReady(payloadSize, bytesRemaining )) {
    this->messageReceived( &data[ length - bytesRemaining ], payloadSize );
    bytesRemaining -= payloadSize;
    bytesHandled += payloadSize + SIZE_OF_LENGTH_FIELD;
    payloadSize = payloadLength( &data[ length - bytesRemaining ], bytesRemaining );
  }
  return bytesHandled;
}
示例#2
0
uint16_t HTTPServer::bodyLength()
{
    if (_bodyPtr == NULL)
        return 0;

    return payloadLength() - (_bodyPtr - (char*)payload());
}
示例#3
0
WSFrameHeaderInfo WSHyBiFrameHeader::info() const {
  WSFrameHeaderInfo inf;
  inf.fin = fin();
  inf.opcode = opcode();
  inf.hasLength = true;
  inf.masked = masked();
  if (masked()) {
    inf.maskingKey.resize(4);
    maskingKey(&inf.maskingKey[0]);
  }
  inf.payloadLength = payloadLength();
  return inf;
}
示例#4
0
boolean HTTPServer::checkRequest(const char* method, const __FlashStringHelper* path)
{
    char* payload = (char*)this->payload();
    uint16_t length = payloadLength();
    uint16_t endOfMethod;
    uint16_t pos;

    // Is there a TCP request ready for us?
    if (!havePacket())
        return false;

    // Does the method match?
    endOfMethod = strlen_P(method);
    if (memcmp_P(payload, method, endOfMethod) != 0) {
        // Wrong method
        return false;
    }

    // Check there is a space after the method
    if (payload[endOfMethod] != ' ') {
        // No space after method
        return false;
    }

    // Do the paths match?
    const char *matchpath = reinterpret_cast<const char *>(path);
    uint8_t maxlen = length - endOfMethod - 1;
    bool allowAny = false;
    _pathPtr = &payload[endOfMethod + 1];
    for(uint8_t i=0; i < maxlen; i++) {
        char match = pgm_read_byte(matchpath + i);

        // Have we got the end of the path section in the HTTP request?
        if (isWhitespace(_pathPtr[i]) || _pathPtr[i] == '?') {
            if (match == '\0') {
                // Both ended at the same time
                _pathPtr[i] = '\0';
                break;
            } else {
                // Path ended before the string we are trying to match against
                return false;
            }
        }

        // Allow anything after a '#' character
        if (match == '#')
            allowAny = true;

        // Allow any character to match a '?'
        if (match == '?' || allowAny)
            continue;

        // Do they match up?
        if (_pathPtr[i] != match)
            return false;
    }

    // Find the start of the body section
    _bodyPtr = NULL;
    for(pos = endOfMethod+1; pos < length-1; pos++) {
        if (payload[pos] == '\n' && (payload[pos-2] == '\n' || payload[pos-1] == '\n')) {
            // Store the location of the start of the body
            _bodyPtr = &payload[pos+1];

            // For convenience NULL-terminate the body
            payload[length] = '\0';
            break;
        }
    }

    return true;
}