Tokenize .htaccess files

走远了吗. 提交于 2019-12-05 16:31:44

.htaccess grammar is actually the exact same as the Apache configuration itself, and example parsers do exist for it.

If you're looking to write your own, you are mostly correct on the format. Remember, section tags can be nested and can have parameters (like <Location />)

English method of parsing:

For each line in the file:
  Strip whitespace from beginning and end of line.
  If the line starts with a '#':
    Parse it as a comment (or skip it)

  Else, If the line starts with a '<':
    If the next character is a '/', the line is a closing tag:
      Seek to the next '>' to get the tag name, and pop it from the tag stack.
    Else, the line is an opening tag:
      Seek to the next '>' for the tag name.
      If the tag, trimmed, contains whitespace:
        Split on the first whitespace. The right side is params, left is the tag. 
        (IfModule, Location, etc use this)

      Push the tag name to the tag stack.

  Else, the line is a directive:
    Split the line on whitespace. This is the directive and params.

Just add quote handling and you're set.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!