Is it really the reading file taking so much time or using $piece on the line and setting it to global too?

There are various things here that may slow you, even $increment (best to be replaced by i+1)

You can also split the reading file and set it to global by two parts, and use $sortbegin 

Try to run your code with %SYS.MONLBL started, it will help you understand where it spends more time.

Your issue with screen formatting is very interesting, it deserves a separate topic. I have not seen any issues yet with it. While I’d like to find a way to improve a standard IRIS terminal, like irissqlcli, I’d like to see the most challenging parts like this.

webTerminal obviously not a right tool for it. As a replacement for it, you can try to use ttyd, I use it for irissqlcli-web

Any reasons for doing this? This is not how it is supposed to be. REST should answer with Status 401, and optionally  with methods available. And web application when gets 401, it knows that it has to authorize the user, and shows its own page or initiates SSO, depending on the task.

Anyway, if really do this way, It looks like %CSP.REST extends %CSP.Login, and it has Method Login

Called for a REST page in the event of a login being required

Did not test it, but I would expect it will do what requested