int M2XStreamClient::readLocation(const char* feedId,
                                  location_read_callback callback,
                                  void* context) {
  if (_client->connect(_host, _port)) {
#ifdef DEBUG
    printf("Connected to M2X server!\n");
#endif
    _client->print("GET /v1/feeds/");
    print_encoded_string(_client, feedId);
    _client->println("/location HTTP/1.0");

    writeHttpHeader(-1);
  } else {
#ifdef DEBUG
    printf("ERROR: Cannot connect to M2X server!\n");
#endif
    return E_NOCONNECTION;
  }
  int status = readStatusCode(false);
  if (status == 200) {
    readLocation(callback, context);
  }

  close();
  return status;
}
int M2XStreamClient::updateLocation(const char* feedId,
                                    const char* name,
                                    const char* latitude,
                                    const char* longitude,
                                    const char* elevation) {
  if (_client->connect(_host, _port)) {
#ifdef DEBUG
    printf("Connected to M2X server!\n");
#endif

    int length = write_location_data(&_null_print, name, latitude, longitude,
                                     elevation);
    _client->print("PUT /v1/feeds/");
    print_encoded_string(_client, feedId);
    _client->println("/location HTTP/1.0");

    writeHttpHeader(length);
    write_location_data(_client, name, latitude, longitude, elevation);
  } else {
#ifdef DEBUG
    printf("ERROR: Cannot connect to M2X server!\n");
#endif
    return E_NOCONNECTION;
  }
  return readStatusCode(true);
}
void M2XStreamClient::writeSendHeader(const char* feedId,
                                      const char* streamName,
                                      int contentLength) {
  _client->print("PUT /v1/feeds/");
  print_encoded_string(_client, feedId);
  _client->print("/streams/");
  print_encoded_string(_client, streamName);
  _client->println(" HTTP/1.0");
  
  writeHttpHeader(contentLength);
}
Ejemplo n.º 4
0
int cacheTofile(lsi_cb_param_t *rec)
{
    MyMData *myData = (MyMData *)g_api->get_module_data(rec->_session, &MNAME, LSI_MODULE_DATA_HTTP);
    if (myData == NULL)
    {
        g_api->log(rec->_session, LSI_LOG_ERROR, "[%s]internal error during cacheTofile.\n", ModuleNameString);
        return 0;
    }
//     if (myData->pEntry == NULL || myData->iCacheState != CE_STATE_WILLCACHE)
//     {
//         g_api->log(rec->_session, LSI_LOG_ERROR, "[%s]cacheTofile error, code[%p %d].\n", 
//                            ModuleNameString, myData->pEntry, myData->iCacheState);
//         clearHooks(rec->_session);
//         return 0;
//     }
    
    myData->pEntry->setMaxStale(myData->pConfig->getMaxStale());
    g_api->log(rec->_session, LSI_LOG_INFO, "[%s]save to %s cachestore, uri:%s\n", ModuleNameString,
                       ((myData->cacheCtrl.isPrivateCacheable()) ? "private" : "public"), myData->orgUri);
    
    int fd = myData->pEntry->getFdStore();
    char *sLastMod = NULL, *sETag = NULL;
    int nLastModLen = 0, nETagLen = 0;
    int headersBufSize = 0;
    CeHeader &CeHeader = myData->pEntry->getHeader();
    CeHeader.m_tmCreated = (int32_t)DateTime_s_curTime;
    CeHeader.m_tmExpire = CeHeader.m_tmCreated + myData->cacheCtrl.getMaxAge();
    getRespHeader(rec->_session, LSI_RESP_HEADER_LAST_MODIFIED, &sLastMod, &nLastModLen);
    if (sLastMod)
        CeHeader.m_tmLastMod = DateTime::parseHttpTime( sLastMod);
    
    getRespHeader(rec->_session, LSI_RESP_HEADER_ETAG, &sETag, &nETagLen);
    if (sETag && nETagLen > 0)
    {
        if (nETagLen > VALMAXSIZE)
            nETagLen = VALMAXSIZE;
        CeHeader.m_offETag = 0;
        CeHeader.m_lenETag = nETagLen;
    }
    else
        CeHeader.m_lenETag = 0;
    
    CeHeader.m_statusCode = g_api->get_status_code(rec->_session);
    
    myData->pEntry->setContentLen(0, 0);  //Wrong number, this will be fixed when publish
    
    if (g_api->is_resp_buffer_gzippped(rec->_session))
        myData->pEntry->markReady(1);
    
    myData->pEntry->saveCeHeader();
    
    if (CeHeader.m_lenETag > 0)
    {
        write(fd, sETag, CeHeader.m_lenETag);
        
#ifdef CACHE_RESP_HEADER            
        myData->m_pEntry->m_sRespHeader.append(sETag, CeHeader.m_lenETag);
#endif
    }
    
    //Stat to write akll other headers
#define MAX_RESP_HEADERS_NUMBER     50
    int count = g_api->get_resp_headers_count( rec->_session );
    if (count >= MAX_RESP_HEADERS_NUMBER)
        g_api->log( rec->_session, LSI_LOG_WARN, "[%s] too many resp headers [=%d]\n",
                            ModuleNameString, count);
    
    struct iovec iov[MAX_RESP_HEADERS_NUMBER];
    count = g_api->get_resp_headers(rec->_session, iov, MAX_RESP_HEADERS_NUMBER);
    for(int i=0; i<count; ++i)
    {
        //check if need to bypass
        if(!checkBypassHeader((const char *)iov[i].iov_base, iov[i].iov_len)) 
#ifdef CACHE_RESP_HEADER                
            headersBufSize += writeHttpHeader(fd, &(myData->m_pEntry->m_sRespHeader), iov[i].iov_base, iov[i].iov_len);
#else
            headersBufSize += writeHttpHeader(fd, NULL,  (char *)iov[i].iov_base, iov[i].iov_len);
#endif
    }
    
#ifdef CACHE_RESP_HEADER        
    if (myData->m_pEntry->m_sRespHeader.len() > 4096)
        myData->m_pEntry->m_sRespHeader.resizeBuf(0);
#endif
    
    myData->pEntry->setContentLen(CeHeader.m_lenETag + headersBufSize, 0);
    
    long iCahcedSize = 0;
    off_t offset = 0;
    const char * pBuf; 
    int len = 0;
    void * pRespBodyBuf = g_api->get_resp_body_buf( rec->_session );
    
    while( !g_api->is_body_buf_eof( pRespBodyBuf, offset ) )
    {
        pBuf = g_api->acquire_body_buf_block(pRespBodyBuf, offset, &len );
        if ( !pBuf )
            break;
        write(fd, pBuf, len);
        g_api->release_body_buf_block( pRespBodyBuf, offset );
        offset += len;
        iCahcedSize += len;
    }
    
    int part1Len = myData->pEntry->getContentTotalLen();
    myData->pEntry->setContentLen(part1Len, iCahcedSize);
    
    if (myData->pEntry->m_sPart3Buf.len() > 8)
    {
        write(fd, myData->pEntry->m_sPart3Buf.c_str(), myData->pEntry->m_sPart3Buf.len());
    }
    
    myData->pDirHashCacheStore->publish(myData->pEntry);
    myData->iCacheState = CE_STATE_CACHED;  //Succeed
    g_api->free_module_data(rec->_session, &MNAME, LSI_MODULE_DATA_HTTP, httpRelease);
    
    g_api->log(rec->_session, LSI_LOG_DEBUG, "[%s:cacheTofile] stored, size %ld\n",
                       ModuleNameString, offset);
    return 0;
}