예제 #1
0
void GopherEngine::fileManagement (const string &fname, const string &path,GopherMenu &tmp)
{
  string ext;
  string f_path = path;
  if (f_path != "")
    f_path.append ("/");
  f_path.append (fname);
  string abs_name = abs_path;
  if (abs_name != "")
    abs_name.append ("/");
  abs_name.append (f_path);

  FilesUtility::getFileExt (ext,fname);
  if (handlers.containsKey (ext.c_str ()))
    (this->*handlers.get(ext.c_str ())) (fname, path, tmp);
  else
    {
      MimeRecord *mime = getMIME (abs_name);
      if (mime != NULL)
        {
          string mymime = mime->mimeType;
          mymime = mymime.substr (0,mymime.find ('/',0));
          if (handlers.containsKey (mymime.c_str ()))
            (this->*handlers.get (mymime.c_str ()))(fname, path, tmp);
        }
      else
        tmp.addItem (GopherFile (fname.c_str (),
                                 f_path.c_str (),
                                 hostname.c_str () ,
                                 port.c_str ()));
    }
}
예제 #2
0
StHandle<StFileNode> StFileNode::detach() const {
    StHandle<StFileNode> aCopy = new StFileNode(getPath());
    aCopy->setMIME(getMIME());
    for(size_t aSubId = 0; aSubId < size(); ++aSubId) {
        const StFileNode* aSubNode = getValue(aSubId);
        aCopy->add(new StFileNode(aSubNode->getSubPath(), aCopy.access()));
    }
    return aCopy;
}
예제 #3
0
void connectionProcessing(int connDescr){
	char buffer[256];
        bzero(buffer, sizeof(buffer));
	FILE *conn = fdopen(connDescr, "r+");
	char requestType[5];
	char *url;
	int nowLine = 0, ptr = 0, emptyStr = 0;
  while(1){
		fgets(buffer, sizeof(buffer), conn);
		if (strlen(buffer) == 0 && nowLine == 0) return;
		ptr = 0;
		if (nowLine == 0){
			//Parse request type
			do {
				requestType[ptr]=buffer[ptr];
			} while (buffer[++ptr]!=' ');
			requestType[ptr] = 0;
			//Parse url
			int urlLength = (int)strlen(buffer)-(int)strlen(requestType)-(int)strlen(PROTOCOL)-4;
			int urlPtr = 0;
			url = (char*)malloc((size_t)(urlLength*sizeof(char))+1);
			ptr++;
			do {
				url[urlPtr++]=buffer[ptr];
				if (urlLength - 1 < urlPtr) break;
				if (ptr - 2 > strlen(buffer)) break;
			} while (buffer[++ptr]!=' ');
			url[urlPtr] = 0;
		} else {
		}
		nowLine++;
		if (strcmp(buffer, "\r\n") == 0){
			break;
		}
	}
	//Add home directory to URL
	int fileLen = (int)(strlen(url)+strlen(homeDirectory)+strlen(DEFAULT_FILE));
	char *fileName = malloc(fileLen*sizeof(char)+1);
	strcpy(fileName, homeDirectory);
	strcat(fileName, url);
	//Try to open file
	if (!strcmp(requestType, "GET")){
		if (fileName[strlen(fileName) - 1] == '/'){
			//Directory request - add index.html
			strcat(fileName, DEFAULT_FILE);
		}
		FILE *respContent = fopen(fileName, "rb");
		if (respContent != NULL){
			fseek(respContent, 0, SEEK_END);
			int contentSize = ftell(respContent);
			rewind(respContent);
			char *response = generateOKResponse(contentSize, getMIME(fileName));
			fputs(response, conn);
			char *respBuffer;
			respBuffer = (char*) malloc (sizeof(char)*contentSize);
      size_t result = fread(respBuffer, 1, contentSize, respContent);
			fwrite(respBuffer, 1, contentSize, conn);
			free(respBuffer);
			fclose(respContent);
		} else {
			char *response = generate404Response();
			fprintf(conn, "%s", response);
		}
	}
	fclose(conn);
	free(fileName);
	free(url);
}