static void
processHeader(const char *  const fieldName,
              char *        const fieldValue,
              TSession *    const sessionP,
              const char ** const errorP,
              uint16_t *    const httpErrorCodeP) {
/*----------------------------------------------------------------------------
   We may modify *fieldValue, and we put pointers to *fieldValue and
   *fieldName into *sessionP.

   We must fix this some day.  *sessionP should point to individual
   malloc'ed strings.
-----------------------------------------------------------------------------*/
    *errorP = NULL;  /* initial assumption */

    if (xmlrpc_streq(fieldName, "connection")) {
        if (xmlrpc_strcaseeq(fieldValue, "keep-alive"))
            sessionP->requestInfo.keepalive = TRUE;
        else
            sessionP->requestInfo.keepalive = FALSE;
    } else if (xmlrpc_streq(fieldName, "host")) {
        if (sessionP->requestInfo.host) {
            xmlrpc_strfree(sessionP->requestInfo.host);
            sessionP->requestInfo.host = NULL;
        }
        parseHostPort(fieldValue, &sessionP->requestInfo.host,
                      &sessionP->requestInfo.port, errorP, httpErrorCodeP);
    } else if (xmlrpc_streq(fieldName, "from"))
        sessionP->requestInfo.from = fieldValue;
    else if (xmlrpc_streq(fieldName, "user-agent"))
        sessionP->requestInfo.useragent = fieldValue;
    else if (xmlrpc_streq(fieldName, "referer"))
        sessionP->requestInfo.referer = fieldValue;
    else if (xmlrpc_streq(fieldName, "range")) {
        if (xmlrpc_strneq(fieldValue, "bytes=", 6)) {
            bool succeeded;
            succeeded = ListAddFromString(&sessionP->ranges, &fieldValue[6]);
            if (!succeeded) {
                xmlrpc_asprintf(errorP, "ListAddFromString() failed for "
                                "\"range: bytes=...\" header value '%s'",
                                &fieldValue[6]);
                *httpErrorCodeP = 400;
            }
        }
    } else if (xmlrpc_streq(fieldName, "cookies")) {
        bool succeeded;
        succeeded = ListAddFromString(&sessionP->cookies, fieldValue);
        if (!succeeded) {
            xmlrpc_asprintf(errorP, "ListAddFromString() failed for "
                            "cookies: header value '%s'", fieldValue);
            *httpErrorCodeP = 400;
        }
    } else if (xmlrpc_streq(fieldName, "expect")) {
        if (xmlrpc_strcaseeq(fieldValue, "100-continue"))
            sessionP->continueRequired = TRUE;
    }
}
示例#2
0
文件: http.c 项目: arssivka/naomech
static void
parseHttpHostPortPath(const char *const hostportpath,
                      const char **const hostP,
                      unsigned short *const portP,
                      const char **const pathP,
                      const char **const errorP) {

    const char *path;

    char *buffer;

    buffer = strdup(hostportpath);

    if (!buffer)
        xmlrpc_asprintf(errorP,
                        "Couldn't get memory for host/port/path buffer");
    else {
        char *const slashPos = strchr(buffer, '/');

        char *hostport;

        if (slashPos) {
            path = xmlrpc_strdupsol(slashPos); /* Includes the initial slash */

            *slashPos = '\0';  /* NUL termination for hostport */
        } else
            path = strdup("*");

        hostport = buffer;

        /* The following interprets the port field without taking into account
           any %HH encoding, as the RFC says may be there.  We ignore that
           remote possibility out of laziness.
        */
        parseHostPort(hostport, hostP, portP, errorP);

        if (*errorP)
            xmlrpc_strfree(path);
        else
            *pathP = path;

        free(buffer);
    }
}
示例#3
0
static void
processHeader(const char * const fieldName,
              char *       const fieldValue,
              TSession *   const sessionP,
              uint16_t *   const httpErrorCodeP) {
/*----------------------------------------------------------------------------
   We may modify *fieldValue, and we put pointers to *fieldValue and
   *fieldName into *sessionP.

   We must fix this some day.  *sessionP should point to individual
   malloc'ed strings.
-----------------------------------------------------------------------------*/
    *httpErrorCodeP = 0;  /* initial assumption */

    if (xmlrpc_streq(fieldName, "connection")) {
        if (xmlrpc_strcaseeq(fieldValue, "keep-alive"))
            sessionP->request_info.keepalive = TRUE;
        else
            sessionP->request_info.keepalive = FALSE;
    } else if (xmlrpc_streq(fieldName, "host"))
        parseHostPort(fieldValue, &sessionP->request_info.host,
                      &sessionP->request_info.port, httpErrorCodeP);
    else if (xmlrpc_streq(fieldName, "from"))
        sessionP->request_info.from = fieldValue;
    else if (xmlrpc_streq(fieldName, "user-agent"))
        sessionP->request_info.useragent = fieldValue;
    else if (xmlrpc_streq(fieldName, "referer"))
        sessionP->request_info.referer = fieldValue;
    else if (xmlrpc_streq(fieldName, "range")) {
        if (xmlrpc_strneq(fieldValue, "bytes=", 6)) {
            abyss_bool succeeded;
            succeeded = ListAddFromString(&sessionP->ranges, &fieldValue[6]);
            *httpErrorCodeP = succeeded ? 0 : 400;
        }
    } else if (xmlrpc_streq(fieldName, "cookies")) {
        abyss_bool succeeded;
        succeeded = ListAddFromString(&sessionP->cookies, fieldValue);
        *httpErrorCodeP = succeeded ? 0 : 400;
    }
}
示例#4
0
int main(int argc, char *argv[])
{
  int sd;
  struct hostent *h;
  struct sockaddr_in sa;
  FILE *sock = 0;
  char *line = 0;
  size_t linelen = 0;
  unsigned int num, sent = 0;

  if (argc < 6 || argc > 7) {
    usage(argv[0]);
    exit(1);
  } else if (argc == 7) {
    if (!parseHostPort(argv[6]) || !parseParms(argv+1)) {
      usage(argv[0]);
      exit(1);
    }
  }

  if ( !(h = gethostbyname(host)) ) {
    herror("gethostbyname");
    exit(1);
  }
  
  if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) {
    perror("socket");
    exit(1);
  }

  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  memcpy(&sa.sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
  
  fprintf(stderr, "Connecting to %s port %hu\n", inet_ntoa(sa.sin_addr), port);

  if ( connect(sd, (struct sockaddr *)&sa, sizeof(sa)) ) {
    perror("connect");
    exit(1);
  }  
  
  if ( !(sock = fdopen(sd, "r+")) ) {
    perror("fdopen");
    exit(1);
  }
  
  fprintf(stderr, "Connected sending file %s size: %d as id: %d\n", filename, bytes, id);

  
  if ( fprintf(sock, "SET SOUND %u %u %u %u %u\n", id, bytes, bits, chans, rate) <= 0 ) {
    fprintf(stderr, "Error writing to socket\n");
    exit(1);
  }
  
  /* Grab the 'READY' */
  if ( getline(&line, &linelen, sock) == -1 ) {
    perror("getline");
    exit(1);
  }
  
  while (!feof(f)) {
    char buf[4096];
    num = fread(buf, 1, sizeof(buf), f);
    if ( fwrite(buf, 1, num, sock) != num ) {
      perror("fwrite");
      exit(1);
    }
    sent += num;
  }

  if ( getline(&line, &linelen, sock) == -1 ) {
    fprintf(stderr, "Did not receive any response, exiting.\n");
    exit(1);
  } else if (strcmp(line, "OK\n")) {
    fprintf(stderr, "Sound rejected.\n");
    exit(1);
  }

  fprintf(stderr, "%u bytes sent, OK.\n", sent);
  fprintf(sock, "EXIT\n");
  fflush(sock);
  free(line);
  shutdown(sd, SHUT_RDWR);
  fclose(sock);
  return 0;
}
示例#5
0
int main(int argc, char *argv[])
{
  int sd;
  struct hostent *h;
  struct sockaddr_in sa;
  FILE *sock = 0;
  char *line = 0, *cmd = 0;
  size_t linelen = 0;
  unsigned int i, j;
  size_t dataSize;
  struct Matrix matrix;


  if (argc > 3 || argc < 2) {
    usage(argv[0]);
    exit(1);
  } else if (argc == 3) {
    if (!parseHostPort(argv[1]))
      exit(1);
    cmd = argv[2];
  } else 
    cmd = argv[1];

  if ( !(h = gethostbyname(host)) ) {
    herror("gethostbyname");
    exit(1);
  }
  
  if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) {
    perror("socket");
    exit(1);
  }

  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  memcpy(&sa.sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
  
  fprintf(stderr, "Connecting to %s port %hu\n", inet_ntoa(sa.sin_addr), port);

  if ( connect(sd, (struct sockaddr *)&sa, sizeof(sa)) ) {
    perror("connect");
    exit(1);
  }  
  
  if ( !(sock = fdopen(sd, "r+")) ) {
    perror("fdopen");
    exit(1);
  }
  
  fprintf(stderr, "Connected, sending '%s'\n", cmd);
  fprintf(sock, "%s\n", cmd);
  fflush(sock);
  
  matrixInit(&matrix);
  
  if ( getline(&line, &linelen, sock) == -1 
       || sscanf(line, "MATRIX %u %u", &matrix.m, &matrix.n) != 2) {
    if (strstr(line, "ERROR") == line) 
      fprintf(stderr, "Got ERROR response.. check the command and try again!\n");
    else
      fprintf(stderr, "Cannot parse matrix dimensions, bailing.\n");
    exit(1);
  }
  
  fprintf(stderr, "Ok, matrix is %ux%u.  Reading matrix data...\n", matrix.m, matrix.n);
  matrix.d = calloc(matrix.m*matrix.n, sizeof(double));  
  
  if (!matrix.d) {
    fprintf(stderr, "Cannot allocate memory.\n");
    exit(1);
  }

  fprintf(sock, "READY\n");
  fflush(sock);
  dataSize = matrix.m*matrix.n*sizeof(double);  
  
  if ( receiveData(matrix.d, dataSize, sock) != (ssize_t)dataSize )  {
    fprintf(stderr, "receive error when reading matrix data, bailing\n");
    exit(1);
  }

  fprintf(stderr, "%u bytes read, OK.\n", dataSize);
  
  if ( getline(&line, &linelen, sock) == -1 ) {
    fprintf(stderr, "Did not receive any response, exiting.\n");
    exit(1);
  } else if (strcmp(line, "OK\n")) {
    fprintf(stderr, "Got OK.\n");
    exit(1);
  }

  for(i = 0; i < matrix.m; ++i ) {
    for (j = 0; j < matrix.n; ++j) 
      fprintf(stdout, "%lf ", *at(&matrix, i, j));
    fprintf(stdout, "\n");
  }
 
  fprintf(sock, "EXIT\n");
  fflush(sock);
  free(line);
  free(matrix.d);
  shutdown(sd, SHUT_RDWR);
  fclose(sock);
  return 0;
}
static void
parseRequestUri(char *           const requestUri,
                const char **    const hostP,
                unsigned short * const portP,
                const char **    const pathP,
                const char **    const queryP,
                uint16_t *       const httpErrorCodeP) {
/*----------------------------------------------------------------------------
  Parse the request URI (in the request line
  "GET http://www.myserver.com:8080/myfile.cgi?parm HTTP/1.1",
  "http://www.myserver.com:8080/myfile.cgi?parm" is the request URI).

  Return as *hostP the "www.myserver.com" in the above example.  If
  that part of the URI doesn't exist, return *hostP == NULL.

  Return as *portP the 8080 in the above example.  If it doesn't exist,
  return 80.

  Return as *pathP the "/myfile.cgi" in the above example.  If it
  doesn't exist, return "*".

  Return as *queryP the "parm" in the above example.  If it doesn't
  exist, return *queryP == NULL.

  Return strings in newly malloc'ed storage.

  Return as *httpErrorCodeP the HTTP error code that describes how the
  URI is invalid, or 0 if it is valid.  If it's invalid, other return
  values are meaningless.

  This destroys 'requestUri'.  We should fix that.
-----------------------------------------------------------------------------*/
    bool error;

    unescapeUri(requestUri, &error);
    
    if (error)
        *httpErrorCodeP = 400;  /* Bad Request */
    else {
        char * requestUriNoQuery;
           /* The request URI with any query (the stuff marked by a question
              mark at the end of a request URI) chopped off.
           */
        {
            /* Split requestUri at the question mark */
            char * const qmark = strchr(requestUri, '?');
            
            if (qmark) {
                *qmark = '\0';
                *queryP = strdup(qmark + 1);
            } else
                *queryP = NULL;

            requestUriNoQuery = requestUri;
        }

        if (requestUriNoQuery[0] == '/') {
            *hostP = NULL;
            *pathP = strdup(requestUriNoQuery);
            *portP = 80;
            *httpErrorCodeP = 0;
        } else {
            if (!xmlrpc_strneq(requestUriNoQuery, "http://", 7))
                *httpErrorCodeP = 400;  /* Bad Request */
            else {
                char * const hostportpath = &requestUriNoQuery[7];
                char * const slashPos = strchr(hostportpath, '/');

                const char * host;
                const char * path;
                unsigned short port;

                char * hostport;
                
                if (slashPos) {
                    char * p;
                    path = strdup(slashPos);
                    
                    /* Nul-terminate the host name.  To make space for
                       it, slide the whole name back one character.
                       This moves it into the space now occupied by
                       the end of "http://", which we don't need.
                    */
                    for (p = hostportpath; *p != '/'; ++p)
                        *(p-1) = *p;
                    *(p-1) = '\0';
                    
                    hostport = hostportpath - 1;
                    *httpErrorCodeP = 0;
                } else {
                    path = strdup("*");
                    hostport = hostportpath;
                    *httpErrorCodeP = 0;
                }
                if (!*httpErrorCodeP) {
                    const char * error;
                    parseHostPort(hostport, &host, &port,
                                  &error, httpErrorCodeP);
                    if (error)
                        xmlrpc_strfree(error);
                    else
                        *httpErrorCodeP = 0;
                }
                if (*httpErrorCodeP)
                    xmlrpc_strfree(path);

                *hostP  = host;
                *portP  = port;
                *pathP  = path;
            }
        }
    }
}
示例#7
0
static void
parseRequestUri(char *           const requestUri,
                const char **    const hostP,
                const char **    const pathP,
                const char **    const queryP,
                unsigned short * const portP,
                uint16_t *       const httpErrorCodeP) {
/*----------------------------------------------------------------------------
  Parse the request URI (in the request line
  "GET http://www.myserver.com/myfile?parm HTTP/1.1",
  "http://www.myserver.com/myfile?parm" is the request URI).

  This destroys *requestUri and returns pointers into *requestUri!

  This is extremely ugly.  We need to redo it with dynamically allocated
  storage.  We should return individual malloc'ed strings.
-----------------------------------------------------------------------------*/
    abyss_bool error;

    unescapeUri(requestUri, &error);
    
    if (error)
        *httpErrorCodeP = 400;  /* Bad Request */
    else {
        char * requestUriNoQuery;
           /* The request URI with any query (the stuff marked by a question
              mark at the end of a request URI) chopped off.
           */
        {
            /* Split requestUri at the question mark */
            char * const qmark = strchr(requestUri, '?');
            
            if (qmark) {
                *qmark = '\0';
                *queryP = qmark + 1;
            } else
                *queryP = NULL;
        }
        
        requestUriNoQuery = requestUri;

        if (requestUriNoQuery[0] == '/') {
            *hostP = NULL;
            *pathP = requestUriNoQuery;
            *portP = 80;
        } else {
            if (!xmlrpc_strneq(requestUriNoQuery, "http://", 7))
                *httpErrorCodeP = 400;  /* Bad Request */
            else {
                char * const hostportpath = &requestUriNoQuery[7];
                char * const slashPos = strchr(hostportpath, '/');
                char * hostport;
                
                if (slashPos) {
                    char * p;
                    *pathP = slashPos;
                    
                    /* Nul-terminate the host name.  To make space for
                       it, slide the whole name back one character.
                       This moves it into the space now occupied by
                       the end of "http://", which we don't need.
                    */
                    for (p = hostportpath; *p != '/'; ++p)
                        *(p-1) = *p;
                    *(p-1) = '\0';
                    
                    hostport = hostportpath - 1;
                    *httpErrorCodeP = 0;
                } else {
                    *pathP = "*";
                    hostport = hostportpath;
                    *httpErrorCodeP = 0;
                }
                if (!*httpErrorCodeP)
                    parseHostPort(hostport, hostP, portP, httpErrorCodeP);
            }
        }
    }
}
示例#8
0
int UnistorBenchConfig::loadConfig(string const & strConfFile){
    CwxIniParse  parser;
    string value;
    //½âÎöÅäÖÃÎļþ
    if (false == parser.load(strConfFile)){
        snprintf(m_szError, 2047, "Failure to Load conf file. err=%s", parser.getErrMsg());
        return -1;
    }

    //load unistor_bench:home
    if (!parser.getAttr("unistor_bench", "home", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:home] for running path.");
        return -1;
    }
	if ('/' != value[value.length()-1]) value +="/";
    m_strWorkDir = value;

	//load unistor_bench:listen
    if (!parser.getAttr("unistor_bench", "listen", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:listen].");
        return -1;
    }
    if (!parseHostPort(value, m_listen)){
        snprintf(m_szError, 2047, "[unistor_bench:listen] is invalid, it should be [ip:port] format.");
        return -1;
    }
    // load echo unistor_bench:conn_num
    if (!parser.getAttr("unistor_bench", "conn_num", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:conn_num].");
        return -1;
    }
    m_unConnNum = strtoul(value.c_str(), NULL, 0);
    // load echo unistor_bench:conn_lasting
    if (!parser.getAttr("unistor_bench", "conn_lasting", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:conn_lasting].");
        return -1;
    }
    m_bLasting = value=="1"?true:false;

    // load echo unistor_bench:data_opr
    if (!parser.getAttr("unistor_bench", "data_opr", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_opr].");
        return -1;
    }
	m_strOpr =value;
	if ((m_strOpr != "import")&&(m_strOpr != "add")&&(m_strOpr != "set")&&(m_strOpr != "update")&&(m_strOpr != "delete")&&(m_strOpr != "get")){
		snprintf(m_szError, 2047, "invalid data opr[%s] for [unistor_bench:data_opr], must be [add/set/update/delete/import/get", m_strOpr.c_str());
		return -1;
	}
    // load echo unistor_bench:data_size
    if (!parser.getAttr("unistor_bench", "data_size", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_size].");
        return -1;
    }
    m_uiDataSize =strtoul(value.c_str(), NULL, 0);
	if (m_uiDataSize > 1024*1024) m_uiDataSize = 1024*1024;

    // load echo unistor_bench:data_key_in_order
    if (!parser.getAttr("unistor_bench", "data_key_in_order", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_key_in_order].");
        return -1;
    }
	m_bKeyOrder = value=="yes"?true:false;

    // load echo unistor_bench:data_get_order
    if (!parser.getAttr("unistor_bench", "data_get_order", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_get_order].");
        return -1;
    }
	m_bGetOrder = value == "yes"?true:false;

    // load echo unistor_bench:data_base
    if (!parser.getAttr("unistor_bench", "data_base", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_base].");
        return -1;
    }
    m_uiDataBase =strtoul(value.c_str(), NULL, 0);
	if (m_uiDataBase < 10000) m_uiDataBase = 10000;
    // load echo unistor_bench:data_mod
    if (!parser.getAttr("unistor_bench", "data_mod", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_mod].");
        return -1;
    }
    m_uiDataMod =strtoul(value.c_str(), NULL, 0);
    // load echo unistor_bench:data_master
    if (!parser.getAttr("unistor_bench", "data_master", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_master].");
        return -1;
    }
    m_bGetMaster = (value == "yes"?true:false);
    // load echo unistor_bench:cache
    if (!parser.getAttr("unistor_bench", "cache", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:cache].");
        return -1;
    }
    m_bCache = (value == "yes"?true:false);

    // load echo unistor_bench:data_group
    if (!parser.getAttr("unistor_bench", "data_group", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_group].");
        return -1;
    }
	m_uiKeyGroup = strtoul(value.c_str(), NULL, 0);
	if (0 == m_uiKeyGroup) m_uiKeyGroup = 1;

    // load echo unistor_bench:data_group_index
    if (!parser.getAttr("unistor_bench", "data_group_index", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:data_group_index].");
        return -1;
    }
	m_uiKeyIndex = strtoul(value.c_str(), NULL, 0);
	if (m_uiKeyIndex >= m_uiKeyGroup) m_uiKeyIndex = m_uiKeyGroup - 1;
    // load echo unistor_bench:expire
    if (!parser.getAttr("unistor_bench", "expire", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:expire].");
        return -1;
    }
    m_uiExpire =strtoul(value.c_str(), NULL, 0);

    // load echo unistor_bench:user
    if (!parser.getAttr("unistor_bench", "user", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:user].");
        return -1;
    }
    m_strUser = value;

    // load echo unistor_bench:passwd
    if (!parser.getAttr("unistor_bench", "passwd", value) || !value.length()){
        snprintf(m_szError, 2047, "Must set [unistor_bench:passwd].");
        return -1;
    }
    m_strPasswd = value;
	return 0;
}