/** This represent a connection : we have a fixed number of them
* fetchOpen links them with servers
* fetchPipe reads those which are linked
*/
//表达连接相关信息
struct Connexion {
char state; // socket状态 : EMPTY, CONNECTING, WRITE, OPEN
int pos; //请求发送位置标记
FetchError err; // 连接如何终止,enum
int socket; // socket descriptor number
int timeout; // 连接超时时间设置
LarbinString request; // http请求报头
file *parser; // 解析连接 (a robots.txt or an html file)
char buffer[maxPageSize];//下载的网页数据
Connexion();//初始化state=emptyC,parser=NULL
~Connexion();//不执行,一旦执行就出错,assert(false)
void recycle();//释放*parser,再次初始化request
};
该结构体的主要两个类成员LarbinStrng,file。
LarbinString类(string.h声明,string.cc实现),主要是对字符串http报头的字符串相关操作
class LarbinString {
private:
char *chaine; //http报头字符串
uint pos; //http报头当前位置标记
uint size; //http报头大小
public:
LarbinString(uint size = STRING_SIZE); //初始化*chaine,pos=0,size
~LarbinString(); //释放*chaine
void recycle(uint size = STRING_SIZE); //重新分配*chaine
char *getString(); //返回*chaine
char *giveString(); //返回*chaine的拷贝
void addChar(char c); //chaine[pos]=c
void addString(char *s); //添加*s到*chine后(pos起)
void addBuffer(char *s, uint len); //添加*s到*chine后(pos起)
inline uint getLength() { return pos; }; //return pos
inline char operator [] (uint i) //数组[]运算符重载
void setChar(uint i, char c); //chaine[i]=c
};
file类,(html类,robots类),二者继承file类。解析连接(robots.txt or .html file)
class file {
protected:
char *buffer; //connexion中的buffer[maxPageSize(100000)],下载的网页数据
char *posParse; //解析位置
public:
file(Connexion *conn); //初始化*buffer=*posParse=conn->buffer;pos=0
virtual ~file();
bool isRobots; // Is it a robots.txt
uint pos; //*buffer的当前位置
// a string arrives from the server
virtual int inputHeaders(int size) = ; // just parse headers
virtual int endInput() = ;
};class html : public file {
private:
url *here; //url地址
char *area; //当前感兴趣区的起始位置
char *contentStart; //真正内容的起始位置,报头之后的内容
url *base; //url基地址
/* manage a new url : verify and send it */
void manageUrl(url *nouv, bool isRedir);