int JNL_HTTPGet::run() { int cnt=0; if (m_http_state==-1||!m_con) return -1; // error run_again: m_con->run(); if (m_con->get_state()==JNL_Connection::STATE_ERROR) { seterrstr(m_con->get_errstr()); return -1; } if (m_con->get_state()==JNL_Connection::STATE_CLOSED) return 1; if (m_http_state==0) // connected, waiting for reply { if (m_con->recv_lines_available()>0) { char buf[4096]; m_con->recv_line(buf,4095); buf[4095]=0; m_reply=(char*)malloc(strlen(buf)+1); strcpy(m_reply,buf); int code=getreplycode(); if (code == 200 || code==206) m_http_state=2; // proceed to read headers normally else if (code == 301 || code==302) { m_http_state=1; // redirect city } else { seterrstr(buf); m_http_state=-1; return -1; } cnt=0; } else if (!cnt++) goto run_again; } if (m_http_state == 1) // redirect { while (m_con->recv_lines_available() > 0) { char buf[4096]; m_con->recv_line(buf,4096); if (!buf[0]) { m_http_state=-1; return -1; } if (!strnicmp(buf,"Location:",9)) { char *p=buf+9; while (*p== ' ') p++; if (*p) { connect(p); return 0; } } } } if (m_http_state==2) { if (!cnt++ && m_con->recv_lines_available() < 1) goto run_again; while (m_con->recv_lines_available() > 0) { char buf[4096]; m_con->recv_line(buf,4096); if (!buf[0]) { m_http_state=3; break; } if (!m_recvheaders) { m_recvheaders_size=strlen(buf)+1; m_recvheaders=(char*)malloc(m_recvheaders_size+1); if (m_recvheaders) { strcpy(m_recvheaders,buf); m_recvheaders[m_recvheaders_size]=0; } } else { int oldsize=m_recvheaders_size; m_recvheaders_size+=strlen(buf)+1; char *n=(char*)malloc(m_recvheaders_size+1); if (n) { memcpy(n,m_recvheaders,oldsize); strcpy(n+oldsize,buf); n[m_recvheaders_size]=0; free(m_recvheaders); m_recvheaders=n; } } } } if (m_http_state==3) { } return 0; }
int JNL_HTTPGet::run() { int cnt=0; if (m_http_state==-1||!m_con) return -1; // error run_again: m_con->run(); if (m_con->get_state()==JNL_Connection::STATE_ERROR) { seterrstr(m_con->get_errstr()); return -1; } if (m_con->get_state()==JNL_Connection::STATE_CLOSED) return 1; if (m_http_state==0) // connected, waiting for reply { if (m_con->recv_lines_available()>0) { char buf[4096]; m_con->recv_line(buf,4095); buf[4095]=0; m_reply=(char*)malloc(strlen(buf)+1); strcpy(m_reply,buf); int code=getreplycode(); if (code == 200 || code==206 || code == 201) m_http_state=2; // proceed to read headers normally else if (code == 301 || code==302) { m_http_state=1; // redirect city } else { seterrstr(buf); m_http_state=-1; m_boy_george = 0; return -1; } cnt=0; } else if (!cnt++) goto run_again; } if (m_http_state == 1) // redirect { while (m_con->recv_lines_available() > 0) { char buf[4096]; m_con->recv_line(buf,4096); if (!buf[0]) { m_boy_george = 0; m_http_state=-1; return -1; } if (!strnicmp(buf,"Location:",9)) { char *p=buf+9; while (*p== ' ') p++; if (*p) { free(m_http_redir_url); if(m_boy_george > MAX_RECURSION_COUNT) { seterrstr(strdup("Too many redirects")); m_http_state=-1; return -1; } m_boy_george++; if(strnicmp(p,"http:",strlen("http:"))) { char *newp; newp = (char*)malloc(strlen(p) + strlen("http://") + strlen(m_http_host) + 1); newp[0] = 0; strcat(newp,"http://"); strcat(newp,m_http_host); strcat(newp,p); m_http_redir_url = newp; p = newp; } else { m_http_redir_url = strdup(p); } connect(p); return 0; } } } } if (m_http_state==2) { if (!cnt++ && m_con->recv_lines_available() < 1) goto run_again; m_boy_george = 0; while (m_con->recv_lines_available() > 0) { char buf[4096]; m_con->recv_line(buf,4096); if (!buf[0]) { m_http_state=3; break; } if (!m_recvheaders) { m_recvheaders_size=strlen(buf)+1; m_recvheaders=(char*)malloc(m_recvheaders_size+1); if (m_recvheaders) { strcpy(m_recvheaders,buf); m_recvheaders[m_recvheaders_size]=0; } } else { int oldsize=m_recvheaders_size; m_recvheaders_size+=strlen(buf)+1; char *n=(char*)malloc(m_recvheaders_size+1); if (n) { memcpy(n,m_recvheaders,oldsize); strcpy(n+oldsize,buf); n[m_recvheaders_size]=0; free(m_recvheaders); m_recvheaders=n; } } } } if (m_http_state==3) { } return 0; }