示例#1
0
static nglString GetEncodingString(TidyNode tnod)
{
  if (tidyNodeGetId(tnod) == TidyTag_META)
  {
    // Search for the encoding attribute
    TidyAttr attr_content = tidyAttrGetById(tnod, TidyAttr_CONTENT);
    TidyAttr attr_httpequiv = tidyAttrGetById(tnod, TidyAttr_HTTP_EQUIV);
    if (attr_content && attr_httpequiv)
    {
      nglString contenttype(tidyAttrValue(attr_content));
      if (contenttype.Compare(_T("content-type"), false) != 0)
      {
        // bleh...
      }
      nglString encoding(tidyAttrValue(attr_content));
      //NGL_OUT(_T("content found in the tree: %s"), encoding.GetChars());
      int32 col = encoding.Find(_T("charset="));
      encoding = encoding.Extract(col + 8);
      //NGL_OUT(_T("encoding found in the tree: %s"), encoding.GetChars());
      return encoding;
    }
  }
  
  TidyNode child;
  
  for (child = tidyGetChild(tnod); child; child = tidyGetNext(child))
  {
    nglString str(GetEncodingString(child));
    if (!str.IsNull())
      return str;
  }
  
  
  return nglString::Null;
}
bool	HtmlModule::doExec(IModuleRequest &request, IModuleClient &client, IModuleResponse &response)
{
    std::cout << "request uri = " << request.getURI() << std::endl;

    response.setVersion(request.getVersion());
    response.setCode(200);
    response.setHeader("Connection", "close");
    std::string		contenttype("text/html");
    //	request.getHeaders("Content-Type", contenttype);
    response.setHeader("Content-Type", contenttype);
    //response.setContent("<html><img src=\"/image.prout\"\></br>\nContent fichier/image/what-else</html>");

    // PATH du fichier GuiKs.htm
    std::string		fp("C://www");
    if (!request.getURI().compare("/"))
        request.setURI(std::string("/index.htm"));
    response.setFilePath(fp + request.getURI());
    response.isTmpFile(true);
    return true;
}
示例#3
0
int TestFD()
{
  CString filename("C:\\TEMP\\mpbuffer.txt");
  FILE* fp = nullptr;
  char buffer[4004];
  CString contenttype("multipart/form-data boundary=\"------WebKitFormBoundaryBFCjeYoxVSC92Luo\"");

  fopen_s(&fp, filename, "rb");
  if (fp)
  {
    fread(buffer, 1, 4000, fp);
    buffer[2063] = 0;
    FileBuffer fb;
    // fb.AddBuffer((uchar*)buffer, 2063);
    fb.SetBuffer((uchar*) buffer, 2063);

    MultiPartBuffer mpb(FD_UNKNOWN);
    mpb.ParseBuffer(contenttype, &fb);

    printf("Number of parts: %d\n",(int) mpb.GetParts());
  }
  return 0;
}
示例#4
0
void
handlerequest(int fd, const char *addr)
{
    char buffer[BUFFER_SIZE];
    char can_uri[BUFFER_SIZE];
    char filepath[BUFFER_SIZE];
    char headers[BUFFER_SIZE];
    char pf[BUFFER_SIZE];
    int len = 0, ret, begin = 0, p, pbegin = 0, plen = 0;
    int state = 0, filelen;

    int fduri;
    char *uribuffer;
    int tot;

    time_t now;
    
    struct header rq;
    rq.code = 500;
    rq.contentlen = 0;
    memset(rq.contenttype, 0, sizeof(rq.contenttype));
    memset(rq.host, 0, sizeof(rq.host));
    memset(rq.cookie, 0, sizeof(rq.cookie));

    do {
        ret = read(fd, buffer + len, BUFFER_SIZE - len);
        if (ret > 0) {
            len += ret;

            while ((state == 0 || state == 1) && begin < len
                    && (p = crlf(buffer + begin, len - begin)) >= 0) {
                p += begin;
                if (state == 0) {
                    if (statusline(fd, buffer + begin, p - begin, &rq))
                        state = 1;
                    else {
                        state = 3;
                        break;
                    }
                } else if (state == 1) {
                    if (p - begin == 0) {
                        if (rq.contentlen == 0)
                            state = 3;
                        else
                            state = 2;
                    } else if (!header(fd, buffer + begin, p - begin, &rq))
                        state = 3;
                }

                begin = p + 2;
                if (state == 2 || state == 3)
                    break;
            }

            if (state == 2) {
                memcpy(rq.payload + pbegin, buffer + begin, len - begin);
                pbegin += len - begin;
                plen += len - begin;
                begin += len - begin;
                
                if (rq.contentlen != 0 && pbegin >= rq.contentlen)
                    state = 3;
            }
        } else {
            state = 3;
            break;
        }
    } while (state <= 2);

    if (rq.contentlen == 0)
        rq.contentlen = plen;

    if (rq.code != 200) {
        respond(fd, rq.code, rq.protocol, NULL, 0, NULL);
    } else {
        if (rq.uri[0] == '/')
            strcpy(can_uri, rq.uri);
        else {
            strcpy(can_uri, "/");
            strcat(can_uri, rq.uri);
        }

        if (!permissible(can_uri)) {
            rq.code = 403;
            respond(fd, rq.code, rq.protocol, NULL, 0, NULL);
        } else {
            state = locateresource(can_uri, filepath, BUFFER_SIZE, &filelen);
            if (state == 0) {
                rq.code = 404;
                respond(fd, rq.code, rq.protocol, NULL, 0, NULL);
            } else if (state == 2) {
                strcpy(buffer, "Location: ");
                strcat(buffer, filepath);
                strcat(buffer, "\r\n");
                rq.code = 301;
                respond(fd, rq.code, rq.protocol, NULL, 0, buffer);
            } else if (state == 3) {
                rq.code = 403;
                respond(fd, rq.code ,rq.protocol, NULL, 0, NULL);
            } else if (state == 1) {
                if (postfix(filepath, pf) && strcmp(pf, "php") == 0) {
                    php(fd, filepath, addr, &rq);
                } else {
                    strcpy(headers, "Content-Type: ");
                    strcat(headers, contenttype(filepath));
                    strcat(headers, "\r\n");

                    if (rq.method == METHOD_HEAD)
                        respond(fd, rq.code, rq.protocol, NULL, filelen, headers);
                    else {
                        fduri = open(filepath, O_RDONLY);
                        if (fduri < 0) {
                            rq.code = 404;
                            respond(fd, rq.code, rq.protocol, NULL, 0, NULL);
                        } else {
                            uribuffer = malloc(filelen);
                            if (uribuffer == 0) {
                                rq.code = 500;
                                respond(fd, rq.code, rq.protocol, NULL, 0, NULL);
                            } else {
                                tot = 0;
                                while (tot < filelen)
                                    tot += read(fduri, uribuffer + tot, filelen - tot);

                                respond(fd, rq.code, rq.protocol, uribuffer, filelen, headers);
                                free(uribuffer);
                            }
                            close(fduri);
                        }
                    }
                }
            }
        }
    }


    now = time(NULL);
    len = strftime(buffer, BUFFER_SIZE, "%d/%b/%Y %H:%M:%S", gmtime(&now));
    fprintf(stdout, "%s - - [%s] \"%s\" %d\n", addr, buffer, rq.statusline, rq.code);
}