jamhttp.cpp

来自「This is a resource based on j2me embedde」· C++ 代码 · 共 672 行 · 第 1/2 页

CPP
672
字号
        } else if (*p == '\r' && p[1] == '\n') {             p += 2;            break;        } else if (   strncmp("Content-length:", p, 15) == 0                    || strncmp("Content-Length:", p, 15) == 0) {            p += 15;            while (*p == ' ') p++;        /* Skip additional space */            contentLength = atoi(p);        } else if (   strncmp("Retry-after:", p, 12) == 0                   || strncmp("Retry-After:", p, 12) == 0) {             p += 12;            while (*p == ' ') p++;        /* Skip additional space */            *retryDelayP = atoi(p);        }        /* Skip to just past the '\r\n' or '\n' */        p = strchr(p, '\n') + 1;    }    if (!readBody) {         closesocket(sock);        return NULL;    }    content = (char*)jam_malloc(contentLength + 1);    content[contentLength] = 0;    /* This saves us some grief on JAM */    /* Set length to be the number of characters we're going to copy     * into the content buffer */    length = length - (p - buffer);    memcpy(content, p, length);    while (length < contentLength) {         int toread = contentLength - length;#ifndef __SYMBIAN32__        if (toread > 1024) {            toread = 1024;        }#endif        retcode = recv(sock, content + length, toread, 0);        if (retcode < 0) {	  if (complain)             PlafErrorMsg("Bad read on socket");	  goto error;        } else if (retcode == 0) { 	  if (complain)             PlafErrorMsg("Unexpectedly reached EOF");	  goto error;        } else {             length += retcode;            PlafDownloadProgress(contentLength, length);        }     }        closesocket(sock);    *contentLengthP = contentLength;    return content;error:    if (sock >= 0) {         closesocket(sock);    }    if (content != NULL) {         jam_free(content);    }    return NULL;}static char *postURL(const char *host, int port, const char *path,	int datalen, char* data,	int *httpCodeP, int *contentLengthP, int *retryDelayP,	int complain){  int sock = -1;  char buffer[1024]; /* we reuse this for sending and receiving the data */  unsigned int length, contentLength;  char *p;  char *content = NULL;  int readBody;  int retcode;    /* Let's set the return args to default values */  *httpCodeP = 404;           /* unable to make a connection */  *contentLengthP = 0;  *retryDelayP = 0;  contentLength = 0;    /* Open the socket and connect to the server */  {    struct hostent* hep;    struct sockaddr_in sin;        memset((void*)&sin, 0, sizeof (sin));    sin.sin_family = AF_INET;    sin.sin_port = htons((short)port);        hep = gethostbyname(host);    if (hep == NULL) {      if (complain) 	PlafErrorMsg("Unable to resolve host name %s\n", host);      return NULL;    }    memcpy(&sin.sin_addr, hep->h_addr, hep->h_length);    sock = socket(PF_INET, SOCK_STREAM, 0);        retcode = connect(sock, (struct sockaddr*)&sin, sizeof (sin));    if (retcode < 0) {      if (complain) 	PlafErrorMsg("Unable to connect to %s:%ld\n", 		     host, (long)port);      goto error;    }  }    sprintf(buffer, "POST %s HTTP/1.0\r\nContent-Length: %d\r\n\r\n", 	  path, datalen);  DO_SEND(sock, buffer, strlen(buffer), 0);  DO_SEND(sock, data, datalen, 0);    /*   * Read as much as we can into "buffer", upto the size of buffer.   * Note that the size of the HTTP header need to be smaller than 1024   * bytes, or else we'd be in trouble.   */  memset(buffer, 0, sizeof(buffer));  for (length = 0; length < sizeof(buffer); ) {     retcode = recv(sock, buffer + length, sizeof(buffer) - length, 0);    if (retcode > 0) {       length += retcode;    } else if (retcode == 0) {       /* EOF */      break;    } else {       if (complain) 	PlafErrorMsg("Error reading socket\n");      goto error;    }   }   if (length <= 5) {     goto error;  }    /* We're assuming that the buffer is long enough to hold the complete   * header.  If not, well something is wrong.      */  p = strchr(buffer, ' ') + 1;        /* Skip past the first space */  while (*p == ' ') p++;              /* Skip additional space */  *httpCodeP = atoi(p);  switch(*httpCodeP) {  case 200:    readBody = TRUE; break;  case 503:    readBody = FALSE; break;  default:    if (complain)       PlafErrorMsg("%s not available", path);    goto error;  }  p = strchr(p, '\n') + 1;  for (;;) {     if (*p == '\n') {       p++;      break;    } else if (*p == '\r' && p[1] == '\n') {       p += 2;      break;    } else if (   strncmp("Content-length:", p, 15) == 0 		  || strncmp("Content-Length:", p, 15) == 0) {      p += 15;      while (*p == ' ') p++;        /* Skip additional space */      contentLength = atoi(p);    } else if (   strncmp("Retry-after:", p, 12) == 0		  || strncmp("Retry-After:", p, 12) == 0) {       p += 12;      while (*p == ' ') p++;        /* Skip additional space */      *retryDelayP = atoi(p);    }    /* Skip to just past the '\r\n' or '\n' */    p = strchr(p, '\n') + 1;  }    if (!readBody) {     closesocket(sock);    return NULL;  }    content = (char*)jam_malloc(contentLength + 1);  content[contentLength] = 0;    /* This saves us some grief on JAM */  /* Set length to be the number of characters we're going to copy   * into the content buffer */  length = length - (p - buffer);  memcpy(content, p, length);    while (length < contentLength) {     int toread = contentLength - length;    if (toread > 1024) {      toread = 1024;    }    retcode = recv(sock, content + length, toread, 0);    if (retcode < 0) {      if (complain) 	PlafErrorMsg("Bad read on socket");	  goto error;    } else if (retcode == 0) { 	  if (complain)             PlafErrorMsg("Unexpectedly reached EOF");	  goto error;    } else {       length += retcode;      PlafDownloadProgress(contentLength, length);    }   }    closesocket(sock);  *contentLengthP = contentLength;  return content;   error:  if (sock >= 0) {     closesocket(sock);  }  if (content != NULL) {     jam_free(content);  }  return NULL;}voidJamHttpInitialize(void) {#ifdef _WIN32_    WSADATA wsaData;    if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {        PlafErrorMsg("Cannot initialize WSA\n");    }#endif}static int setNonBlock(int fd) {#ifdef _UNIX_  return fcntl(fd, F_SETFL, O_NONBLOCK); #endif#ifdef _WIN32_  unsigned long argp = 1;  return ioctlsocket(fd, FIONBIO, &argp);#endif#ifdef __SYMBIAN32__  return -1;#endif}static struct sockaddr_in addr;int JamListen(int port, int block) {      static int servfd = -1;   memset(&addr, 0, sizeof(addr));   addr.sin_family = PF_INET;   addr.sin_addr.s_addr = htonl(INADDR_ANY);   addr.sin_port = htons((short)port);   if (servfd == -1) {     servfd = socket(PF_INET, SOCK_STREAM, 0);     int one = 1;     setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR,                 (char *) &one, sizeof(int));     if (!block) {         setNonBlock(servfd);     }     if (bind(servfd,	      (struct sockaddr*)&addr,	      sizeof(struct sockaddr_in)) < 0) {       servfd = -1;     }   }      if (servfd != -1) {     listen(servfd, 5);     return servfd;   }   return -1;}// Win32 haven't heard about POSIX#ifdef _WIN32_#define socklen_t int#endifint      JamAccept(int servfd, int block) {  int len = sizeof(struct sockaddr_in);  int fd  = accept(servfd,		   (struct sockaddr*)&addr,		   (socklen_t *)&len);  if (fd < 0) return -1;  if (!block) setNonBlock(fd);  return fd;}#ifndef __SYMBIAN32__int JamSelect(int fd, int timeout) {  struct timeval tmo;  fd_set rsocks;  if (timeout >= 0) {    tmo.tv_sec = (timeout / 1000);    tmo.tv_usec = (timeout -  tmo.tv_sec*1000) * 1000;  }  FD_ZERO(&rsocks);  FD_SET(fd, &rsocks);      return select(fd + 1, &rsocks, NULL, NULL,		timeout < 0 ? NULL : &tmo);}#endifint JamRead(int fd, char* buf, int maxlen) {  int rv, len = 0;  do {    rv = recv(fd, buf + len, maxlen - len, 0);    if (rv > 0) {       len += rv;    }  } while (rv > 0 && len <= maxlen);  #ifdef _UNIX_  if ((rv < 0 && errno != EAGAIN) || (rv == 0 && errno == EAGAIN)) {    closesocket(fd);    return -1;  }#endif  return len;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?