【发布时间】:2023-03-10 03:57:01
【问题描述】:
我正在编写一些解析 Apache 配置文件的代码。
正在解析的文件:
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "c:/Apache2/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "c:/Apache2/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
这是解析器方法filePath是上面文件的路径。
public void readFile(string filePath)
{
/* read the file path provided */
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
/* remove the comments from the file */
string[] lines = content.Split('\n');
string newContent = "";
foreach(string line in lines){
char[] chars = line.ToCharArray();
if(chars[0] != '#'){
newContent += line+"\n";
}
}
/* search for Virtual hosts and save them */
string regEx = "<VirtualHost [^>]*>[^<]*</VirtualHost>";
MatchCollection coll = Regex.Matches(newContent, regEx, RegexOptions.IgnoreCase);
string[] vhosts = new string[coll.Count];
int counter = 0;
/* go though them and save them into a string array */
foreach (Match match in coll)
{
vhosts[counter] = (match.Value);
counter++;
}
/* set the number of vhosts */
hosts = new vhost[vhosts.Count()];
/* go though the string array */
for (int c = 0; c < vhosts.Count(); c++ )
{
MessageBox.Show("hi");
/* set the host string to current vhost string */
string host = vhosts[c];
/* get the lines from the vhost */
string[] lines2 = host.Split('\n');
/* create the new save object in the array of vhosts */
hosts[c] = new vhost();
/* tell the console what were proccessing */
Console.WriteLine((c+1).ToString() + " of " + vhosts.Count().ToString());
/* for each line add the rule to the correct vHost via the counter for looping though the vhosts */
for (int i = 0; i < lines2.Count(); i++)
{
/* set the string line to the line were currently on */
string line = lines2[i];
/* get rid of whitespace and split on the space (' ')*/
string[] param = line.Trim().Split(' ');
/* provde the current vHost number and the key and value in the config file */
hosts[c].addRule(param[0], param[1]);
}
}
}
hosts 是vHost[] vHost 如下
class vhost
{
public vhostRules[] rules = new vhostRules[1];
public string getRuleValue(string key)
{
vhostRules[] currentRules = rules;
rules = new vhostRules[currentRules.Count()];
for (int i = 0; i < currentRules.Count(); i++)
{
vhostRules Rule = currentRules[i];
if (key == Rule.key)
return Rule.value;
}
return "";
}
public vhostRules getRule(string key)
{
vhostRules[] currentRules = rules;
rules = new vhostRules[currentRules.Count()];
for (int i = 0; i < currentRules.Count(); i++)
{
vhostRules Rule = currentRules[i];
if (key == Rule.key)
return Rule;
}
return new vhostRules();
}
public vhostRules[] getRules()
{
return rules;
}
public void addRule(string key, string rule)
{
vhostRules[] currentRules = rules;
rules = new vhostRules[currentRules.Count()];
int counter = 0;
for (int i = 0; i < currentRules.Count(); i++)
{
vhostRules Rule = currentRules[i];
rules[i] = Rule;
counter++;
}
rules[counter] = new vhostRules();
rules[counter].key = key;
rules[counter].value = rule;
}
public void removeRule(string key)
{
vhostRules[] currentRules = rules;
rules = new vhostRules[currentRules.Count()];
for (int i = 0; i < currentRules.Count(); i++)
{
vhostRules Rule = currentRules[i];
if (key != Rule.key)
rules[i] = Rule;
}
}
public void changeRule(string key, string value)
{
vhostRules[] currentRules = rules;
rules = new vhostRules[currentRules.Count()];
for (int i = 0; i < currentRules.Count(); i++)
{
vhostRules Rule = currentRules[i];
if (key != Rule.key)
{
rules[i] = Rule;
rules[i].value = value;
}
}
}
}
vhostRules 对象
class vhostRules { public string key; public string value; }
而vhostRules基本上就是一个保存key和value的类
现在问题是上面读取文件的hosts[c].addRule(param[0], param[1]);,您可以看到它在vhost 类中再次调用addRule,即使我清空addRule,所以里面没有代码仍然不起作用。
这是我可以显示问题的唯一方法,没有更小的方法可以复制,因为错误似乎与编译的最终结果有关
我发现的唯一方法是注释或从我的代码中删除hosts[c].addRule(param[0], param[1]); 然后它可以工作,但是当我清空它时,它不起作用,理论上是不正确的
单步执行代码并没有发生错误,它只运行一次循环,
没有错误发生,包括没有无声的死亡,没有例外,没有冻结/崩溃
更新 更新阅读器
try
{
hosts[c].addRule(param[0], param[1]);
}catch (Exception e) { Console.WriteLine(e.Message); }
提供输出
1 of 2
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
2 of 2
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
断点信息
param = {string[2]}
[0] = "ServerAdmin"
[1] = "webmaste
r@dummy-host.example.com"
【问题讨论】:
-
“除了不运行代码没有问题”。男孩,我认为这是大多数无法运行的代码的问题!
-
这是一个公平的评论:“我的车没有任何问题,只是引擎无法启动”。
-
另外,我强烈建议您发布代码的相关部分...我不会等待 45 秒才能从 MegaUpload 下载某些内容,以便能够开始帮助您没有问题的代码。
-
所以向我们展示
relevant部分,它不可能是整个事情都不起作用。尝试在尽可能小的示例中重现它并发布它。 -
@Barkermn01:如果你的应用程序那么大,那么没有人会仔细查看它。尝试生成一个 SSCCE 来证明您的问题。在这样做的过程中,您不妨找出问题所在。