bool AdditionalHeader::Load()

in src/addhead.cpp [62:156]


bool AdditionalHeader::Load(const char* file)
{
    if(!file){
        S3FS_PRN_WARN("file is NULL.");
        return false;
    }
    Unload();

    std::ifstream AH(file);
    if(!AH.good()){
        S3FS_PRN_WARN("Could not open file(%s).", file);
        return false;
    }

    // read file
    std::string line;
    ADDHEAD *paddhead;
    while(getline(AH, line)){
        if(line.empty()){
            continue;
        }
        if('#' == line[0]){
            continue;
        }
        // load a line
        std::istringstream ss(line);
        std::string        key;           // suffix(key)
        std::string        head;          // additional HTTP header
        std::string        value;         // header value
        if(0 == isblank(line[0])){
            ss >> key;
        }
        if(ss){
            ss >> head;
            if(ss && static_cast<size_t>(ss.tellg()) < line.size()){
                value = line.substr(static_cast<int>(ss.tellg()) + 1);
            }
        }

        // check it
        if(head.empty()){
            if(key.empty()){
                continue;
            }
            S3FS_PRN_ERR("file format error: %s key(suffix) is no HTTP header value.", key.c_str());
            Unload();
            return false;
        }

        paddhead = new ADDHEAD;
        if(0 == strncasecmp(key.c_str(), ADD_HEAD_REGEX, strlen(ADD_HEAD_REGEX))){
            // regex
            if(key.size() <= strlen(ADD_HEAD_REGEX)){
                S3FS_PRN_ERR("file format error: %s key(suffix) does not have key std::string.", key.c_str());
                delete paddhead;
                continue;
            }
            key.erase(0, strlen(ADD_HEAD_REGEX));

          // compile
          regex_t*  preg = new regex_t;
          int       result;
          if(0 != (result = regcomp(preg, key.c_str(), REG_EXTENDED | REG_NOSUB))){ // we do not need matching info
              char    errbuf[256];
              regerror(result, preg, errbuf, sizeof(errbuf));
              S3FS_PRN_ERR("failed to compile regex from %s key by %s.", key.c_str(), errbuf);
              delete preg;
              delete paddhead;
              continue;
          }

          // set
          paddhead->pregex     = preg;
          paddhead->basestring = key;
          paddhead->headkey    = head;
          paddhead->headvalue  = value;

        }else{
            // not regex, directly comparing
            paddhead->pregex     = NULL;
            paddhead->basestring = key;
            paddhead->headkey    = head;
            paddhead->headvalue  = value;
        }

        // add list
        addheadlist.push_back(paddhead);

        // set flag
        if(!is_enable){
            is_enable = true;
        }
    }
    return true;
}